You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/plugins/config.rst
+210-1Lines changed: 210 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -699,4 +699,213 @@ Configure parameters that are consumable through Mautic's ``CoreParameterHelper`
699
699
700
700
.. 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``.
701
701
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.
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.
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:
0 commit comments