Skip to content

Commit 8f00a09

Browse files
committed
docs: updated documentation for Configuration component.
1 parent 40ca074 commit 8f00a09

File tree

1 file changed

+210
-1
lines changed

1 file changed

+210
-1
lines changed

docs/plugins/config.rst

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,213 @@ Configure parameters that are consumable through Mautic's ``CoreParameterHelper`
699699
700700
.. note:: The default value must match the value's type for Mautic to typecast and transform appropriately. For example, if there isn't a specific default value to declare, define an empty array, ``[]``, for an array type; zero, ``0``, for an integer type; ``TRUE`` or ``FALSE`` for boolean types; and so forth. Services leveraging parameters should accept and handle ``NULL`` for integer and string types, excluding ``0``.
701701

702-
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information.
702+
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information.
703+
704+
705+
Custom config parameters
706+
************************
707+
Mautic plugins can define custom configuration parameters for use within their code. These parameters are typically stored in ``app/config/local.php``, and their default values should be defined in the plugin’s own config file to ensure stability and avoid errors.
708+
709+
To prevent Symfony from throwing errors during cache compilation, or when accessing parameters directly from the container without checking for existence, always define custom parameters in the `plugin’s config file <https://devdocs.mautic.org/en/latest/plugins/config.html#parameters-config-items>`_. This guarantees the parameter exists and has a fallback value.
710+
711+
To add these configuration options in the Configuration page, you’ll need:
712+
713+
- An `event subscriber <https://devdocs.mautic.org/en/latest/plugins/event_listeners.html>`_ to register the configuration,
714+
- A `form type <https://devdocs.mautic.org/en/latest/components/forms.html>`_ that defines the fields, and
715+
- A specific view for rendering the form.
716+
717+
.. note::
718+
719+
To translate the plugin’s tab label in the Configuration form, include a translation key like ``mautic.config.tab.helloworld_config`` in the plugin’s ``messages.ini`` file. Replace ``helloworld_config`` with the formAlias used when registering the form in the event subscriber.
720+
721+
722+
Config event subscriber
723+
=======================
724+
725+
This allows plugins to interact with Mautic's configuration events. It listens to two important events: ``ConfigEvents::CONFIG_ON_GENERATE`` and ``ConfigEvents::CONFIG_PRE_SAVE``.
726+
727+
The following code example demonstrates how the event subscriber is structured in a plugin.
728+
729+
.. code-block:: php
730+
731+
<?php
732+
// plugins/HelloWorldBundle/EventListener/ConfigSubscriber.php
733+
734+
namespace MauticPlugin\HelloWorldBundle\EventListener;
735+
736+
use Mautic\ConfigBundle\Event\ConfigEvent;
737+
use Mautic\CoreBundle\EventListener\CommonSubscriber;
738+
use Mautic\ConfigBundle\ConfigEvents;
739+
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
740+
741+
/**
742+
* Class ConfigSubscriber
743+
*/
744+
class ConfigSubscriber extends CommonSubscriber
745+
{
746+
/**
747+
* @return array
748+
*/
749+
static public function getSubscribedEvents()
750+
{
751+
return [
752+
ConfigEvents::CONFIG_ON_GENERATE => ['onConfigGenerate', 0],
753+
ConfigEvents::CONFIG_PRE_SAVE => ['onConfigSave', 0]
754+
];
755+
}
756+
757+
/**
758+
* @param ConfigBuilderEvent $event
759+
*/
760+
public function onConfigGenerate(ConfigBuilderEvent $event)
761+
{
762+
$event->addForm(
763+
[
764+
'formAlias' => 'helloworld_config',
765+
'formTheme' => 'HelloWorldBundle:FormTheme\Config',
766+
'parameters' => $event->getParametersFromConfig('HelloWorldBundle')
767+
]
768+
);
769+
}
770+
771+
/**
772+
* @param ConfigEvent $event
773+
*/
774+
public function onConfigSave(ConfigEvent $event)
775+
{
776+
/** @var array $values */
777+
$values = $event->getConfig();
778+
779+
// Manipulate the values
780+
if (!empty($values['helloworld_config']['custom_config_option'])) {
781+
$values['helloworld_config']['custom_config_option'] = htmlspecialchars($values['helloworld_config']['custom_config_option']);
782+
}
783+
784+
// Set updated values
785+
$event->setConfig($values);
786+
}
787+
}
788+
789+
790+
Subscribed events
791+
-----------------
792+
793+
The event subscriber listens to the following events:
794+
795+
- ``ConfigEvents::CONFIG_ON_GENERATE``:
796+
This event is dispatched when the configuration form is built, allowing the plugin to inject its own tab and configuration options.
797+
798+
- ``ConfigEvents::CONFIG_PRE_SAVE``:
799+
This event is triggered before the form values are rendered and saved to the ``local.php`` file. It gives the plugin an opportunity to clean up or manipulate the data before it is written.
800+
801+
Handling configuration generation
802+
---------------------------------
803+
804+
To register the plugin’s configuration details during the ``ConfigEvents::CONFIG_ON_GENERATE`` event, the plugin must call the ``addForm()`` method on the ``ConfigBuilderEvent`` object. The method expects an array with the following elements:
805+
806+
.. list-table::
807+
:header-rows: 1
808+
809+
* - Key
810+
- Description
811+
* - ``formAlias``
812+
- The alias of the form type class that defines the expected form elements.
813+
* - ``formTheme``
814+
- The view that formats the configuration form elements, e.g., ``HelloWorldBundle:FormTheme\Config``.
815+
* - ``parameters``
816+
- An array of custom config elements. You can use ``$event->getParametersFromConfig('HelloWorldBundle')`` to retrieve them from the plugin’s config file.
817+
818+
Manipulating configuration before save
819+
--------------------------------------
820+
821+
To manipulate or clean up the form data before it is saved, use the ``ConfigEvents::CONFIG_PRE_SAVE`` event. This event is triggered just before the values are saved into the ``local.php`` file. It allows the plugin to adjust the values before they are written.
822+
823+
Registering the subscriber
824+
--------------------------
825+
826+
Remember that the subscriber must be registered through the plugin’s configuration in the ``services[events]`` `section <https://devdocs.mautic.org/en/latest/plugins/config.html#service-config-items>`_. This ensures that the plugin listens for the events and reacts accordingly.
827+
828+
829+
Config form
830+
=============
831+
832+
The form type is used to generate the form fields in the main configuration form. Refer to the `Forms documentation <https://devdocs.mautic.org/en/latest/components/forms.html>`_ for more information on using form types.
833+
834+
Remember that the form type must be registered through the plugin’s config in the ``services[forms]`` `section <https://devdocs.mautic.org/en/latest/plugins/config.html#service-config-items>`_
835+
.
836+
837+
Below is an example of a form type class that adds a custom configuration option to the plugin's configuration form.
838+
839+
.. code-block:: php
840+
841+
<?php
842+
// plugins/HelloWorldBundle/Form/Type/ConfigType.php
843+
844+
namespace MauticPlugin\HelloWorldBundle\Form\Type;
845+
846+
use Symfony\Component\Form\AbstractType;
847+
use Symfony\Component\Form\FormBuilderInterface;
848+
849+
/**
850+
* Class ConfigType
851+
*/
852+
class ConfigType extends AbstractType
853+
{
854+
/**
855+
* @param FormBuilderInterface $builder
856+
* @param array $options
857+
*/
858+
public function buildForm(FormBuilderInterface $builder, array $options)
859+
{
860+
$builder->add(
861+
'custom_config_option',
862+
'text',
863+
array(
864+
'label' => 'plugin.helloworld.config.custom_config_option',
865+
'data' => $options['data']['custom_config_option'],
866+
'attr' => array(
867+
'tooltip' => 'plugin.helloworld.config.custom_config_option_tooltip'
868+
)
869+
)
870+
);
871+
}
872+
873+
/**
874+
* {@inheritdoc}
875+
*/
876+
public function getName()
877+
{
878+
return 'helloworld_config';
879+
}
880+
}
881+
882+
Config template
883+
=============
884+
885+
Registering a form theme as ``HelloWorldBundle:FormTheme\Config`` in the event listener tells the ConfigBundle to look in the HelloWorldBundle’s ``Views/FormTheme/Config`` folder for templates. Specifically, it will look for a template named ``_config_{formAlias}_widget.html.php``, where ``{formAlias}`` is the same as the ``formAlias`` set in the plugin’s ``ConfigEvents::CONFIG_ON_GENERATE`` event listener.
886+
887+
The template should be structured in a panel format to match the rest of the configuration UI.
888+
889+
Below is an example of how the template should be structured:
890+
891+
.. code-block:: php
892+
893+
<?php
894+
// plugins/HelloWorldBundle/Views/FormTheme/Config/_config_helloworld_config_widget.html.php
895+
896+
?>
897+
898+
<div class="panel panel-primary">
899+
<div class="panel-heading">
900+
<h3 class="panel-title"><?php echo $view['translator']->trans('mautic.config.tab.helloworld_config'); ?></h3>
901+
</div>
902+
<div class="panel-body">
903+
<?php foreach ($form->children as $f): ?>
904+
<div class="row">
905+
<div class="col-md-6">
906+
<?php echo $view['form']->row($f); ?>
907+
</div>
908+
</div>
909+
<?php endforeach; ?>
910+
</div>
911+
</div>

0 commit comments

Comments
 (0)