Skip to content

Commit b53c477

Browse files
authored
Merge pull request #237 from Teebarh/toyibat
docs: updated documentation for Configuration component.
2 parents 241b878 + 72a84cb commit b53c477

File tree

3 files changed

+207
-2
lines changed

3 files changed

+207
-2
lines changed

docs/_static/tablefix.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
margin-bottom: 24px;
88
max-width: 100%;
99
overflow: visible;
10+
white-space: normal !important;
11+
word-break: break-word !important;
1012
}
1113

1214
.wy-table-responsive th p {
1315
margin-bottom: unset;
14-
}
16+
}
17+

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
# relative to this directory. They are copied after the builtin static files,
7575
# so a file named "default.css" will overwrite the builtin "default.css".
7676
html_static_path = ['_static']
77+
html_css_files = ['tablefix.css']
78+
7779

7880
# Please add links here that do not pass the "make checklinks" check.
7981
# A little context on the reason for ignoring is greatly appreciated!

docs/plugins/config.rst

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Mautic recognizes the Plugin through the general config options.
2222
2323
.. list-table::
2424
:header-rows: 1
25+
:widths: 15 15 40
2526

2627
* - Key
2728
- Type
@@ -99,6 +100,7 @@ The following firewalls are available to routes.
99100

100101
.. list-table::
101102
:header-rows: 1
103+
:widths: 15 20 65
102104

103105
* - Key
104106
- URL prefix
@@ -127,8 +129,11 @@ Route definitions
127129

128130
Route definitions define the route's method, path, controller, parameters, and others defined below.
129131

132+
133+
130134
.. list-table::
131135
:header-rows: 1
136+
:widths: 28 20 25 60
132137

133138
* - Key
134139
- Is required?
@@ -170,6 +175,7 @@ Mautic defaults the following route definitions if not declared otherwise by the
170175

171176
.. list-table::
172177
:header-rows: 1
178+
:widths: 15 20 50
173179

174180
* - Parameter
175181
- Default value
@@ -290,6 +296,7 @@ There are currently four menus built into Mautic.
290296

291297
.. list-table::
292298
:header-rows: 1
299+
:widths: 25 50
293300

294301
* - Key
295302
- Description
@@ -321,6 +328,7 @@ Key each item with its respective :ref:`language string key<plugins/translations
321328

322329
.. list-table::
323330
:header-rows: 1
331+
:widths: 25 15 25 50
324332

325333
* - Key
326334
- Is required?
@@ -489,6 +497,7 @@ For convenience, Mautic auto-tags services defined within specific keys.
489497

490498
.. list-table::
491499
:header-rows: 1
500+
:widths: 15 15 50
492501

493502
* - Key
494503
- Tag
@@ -522,6 +531,7 @@ Key each service with a unique name to all of Mautic, including other Plugins.
522531

523532
.. list-table::
524533
:header-rows: 1
534+
:widths: 25 17 15 50
525535

526536
* - Key
527537
- Is required?
@@ -605,6 +615,7 @@ Mautic uses the follow tags to register services as described below.
605615

606616
.. list-table::
607617
:header-rows: 1
618+
:widths: 35 27 50
608619

609620
* - Tag
610621
- Supported tag arguments
@@ -626,6 +637,7 @@ Mautic uses the follow tags to register services as described below.
626637

627638
.. list-table::
628639
:header-rows: 1
640+
:widths: 25 15 50
629641

630642
* - Tag
631643
- Supported tag arguments
@@ -638,6 +650,7 @@ Mautic uses the follow tags to register services as described below.
638650

639651
.. list-table::
640652
:header-rows: 1
653+
:widths: 40 20 50
641654

642655
* - Tag
643656
- Supported tag arguments
@@ -699,4 +712,191 @@ Configure parameters that are consumable through Mautic's ``CoreParameterHelper`
699712
700713
.. 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``.
701714

702-
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information.
715+
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information.
716+
717+
718+
Custom config parameters
719+
************************
720+
721+
You can define custom configuration parameters in your Plugin to support configurable features, such as enabling or disabling functions.
722+
723+
Mautic Plugins allow you to define these parameters for use within your Plugin’s code. Store these parameters in ``config/local.php``, and define their default values in the Plugin’s own config file to ensure stability and avoid errors.
724+
725+
To avoid errors during cache compilation or when accessing parameters directly from the container without checking for their existence, always define custom parameters in the :ref:`plugins/config:Parameters config items`. This guarantees that the parameter exists and has a fallback value.
726+
727+
To add these configuration options in Mautic's configuration section, you’ll need:
728+
729+
- An :doc:`event subscriber </plugins/event_listeners>` to register the configuration.
730+
- A :doc:`Form type </components/forms>` that defines the fields.
731+
- A specific view for rendering the form.
732+
733+
.. note::
734+
735+
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.
736+
737+
738+
Config event subscriber
739+
=======================
740+
741+
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``.
742+
743+
The following code example shows how a Plugin structures its event subscriber.
744+
745+
.. code-block:: php
746+
747+
<?php
748+
749+
declare(strict_types=1);
750+
751+
namespace MauticPlugin\HelloWorldBundle\EventListener;
752+
use Mautic\ConfigBundle\Event\ConfigEvent;
753+
use Mautic\ConfigBundle\ConfigEvents;
754+
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
755+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
756+
757+
final class ConfigSubscriber extends EventSubscriberInterface
758+
{
759+
/**
760+
* @return mixed[]
761+
*/
762+
static public function getSubscribedEvents(): array
763+
{
764+
return [
765+
ConfigEvents::CONFIG_ON_GENERATE => ['onConfigGenerate', 0],
766+
ConfigEvents::CONFIG_PRE_SAVE => ['onConfigSave', 0]
767+
];
768+
}
769+
770+
public function onConfigGenerate(ConfigBuilderEvent $event): void
771+
{
772+
$event->addForm(
773+
[
774+
'formAlias' => 'helloworld_config',
775+
'formTheme' => 'HelloWorldBundle:FormTheme\Config',
776+
'parameters' => $event->getParametersFromConfig('HelloWorldBundle')
777+
]
778+
);
779+
}
780+
781+
public function onConfigSave(ConfigEvent $event): void
782+
{
783+
/** @var array $values */
784+
$values = $event->getConfig();
785+
// Manipulate the values
786+
if (!empty($values['helloworld_config']['custom_config_option'])) {
787+
$values['helloworld_config']['custom_config_option'] = htmlspecialchars($values['helloworld_config']['custom_config_option']);
788+
}
789+
// Set updated values
790+
$event->setConfig($values);
791+
}
792+
}
793+
794+
795+
Subscribed events
796+
-----------------
797+
798+
The event subscriber listens to the following events:
799+
800+
- ``ConfigEvents::CONFIG_ON_GENERATE``:
801+
Mautic dispatches this event when it builds the configuration form. This allows the Plugin to inject its own tab and configuration options.
802+
803+
- ``ConfigEvents::CONFIG_PRE_SAVE``:
804+
Mautic triggers this event before it renders the form values and saves them to the ``local.php`` file. This allows the Plugin to clean up or modify the data before writing it to ``local.php``.
805+
806+
Generate plugin configuration
807+
-----------------------------
808+
.. vale on
809+
To register Plugin’s configuration details during the ``ConfigEvents::CONFIG_ON_GENERATE event``, call the ``addForm()`` method on the ``ConfigBuilderEvent`` object. The method expects an array with the following elements:
810+
811+
.. list-table::
812+
:header-rows: 1
813+
:widths: 15 50
814+
815+
* - Key
816+
- Description
817+
* - ``formAlias``
818+
- The alias of the form type class that defines the expected form elements.
819+
* - ``formTheme``
820+
- The view that formats the configuration form elements, for example, ``HelloWorldBundle:FormTheme\Config``.
821+
* - ``parameters``
822+
- An array of custom configuration elements. ``Use $event->getParametersFromConfig('HelloWorldBundle')`` to retrieve them from the plugin’s configuration file.
823+
.. vale off
824+
825+
Modify configuration before saving
826+
----------------------------------
827+
828+
To modify the form data before saving, use the ``ConfigEvents::CONFIG_PRE_SAVE event``. This event is triggered just before values are saved to the ``local.php`` file, allowing the Plugin to adjust them.
829+
830+
Register the event subscriber
831+
-----------------------------
832+
833+
Register the subscriber through the Plugin’s configuration in the ``services[events]`` in :ref:`plugins/config:Service config items`. This ensures that the plugin listens for the events and reacts accordingly.
834+
835+
836+
Config form
837+
===========
838+
839+
The form type is used to generate the form fields in the main configuration form. See the :doc:`Forms documentation</components/forms>` for more information about using form types.
840+
841+
Remember that the form type must be registered through the Plugin’s config in the ``services[forms]`` in :ref:`plugins/config:Service config items`
842+
.
843+
844+
Below is an example of a form type class that adds a custom configuration option to the Plugin's configuration form.
845+
846+
.. code-block:: php
847+
848+
<?php
849+
// plugins/HelloWorldBundle/Form/Type/ConfigType.php
850+
851+
namespace MauticPlugin\HelloWorldBundle\Form\Type;
852+
853+
use Symfony\Component\Form\AbstractType;
854+
use Symfony\Component\Form\FormBuilderInterface;
855+
856+
final class ConfigType extends AbstractType
857+
{
858+
/**
859+
* @param mixed[] $options
860+
*/
861+
public function buildForm(FormBuilderInterface $builder, array $options): void
862+
{
863+
$builder->add(
864+
'custom_config_option',
865+
'text',
866+
[
867+
'label' => 'plugin.helloworld.config.custom_config_option',
868+
'data' => $options['data']['custom_config_option'],
869+
'attr' => [
870+
'tooltip' => 'plugin.helloworld.config.custom_config_option_tooltip'
871+
]
872+
]
873+
);
874+
}
875+
}
876+
877+
Config template
878+
===============
879+
880+
Registering a form theme as ``HelloWorldBundle:FormTheme\Config`` in the event listener tells the ConfigBundle to look in the HelloWorldBundle’s ``Resources/views/FormTheme/Config`` folder for templates. Specifically, it will look for a template named ``_config_{formAlias}_widget.html.twig``, where ``{formAlias}`` is the same as the ``formAlias`` set in the Plugin’s ``ConfigEvents::CONFIG_ON_GENERATE`` event listener.
881+
882+
The template should be structured in a panel format to match the rest of the configuration UI.
883+
884+
Below is an example of how the template should be structured:
885+
886+
.. code-block:: twig
887+
888+
{# plugins/HelloWorldBundle/Views/FormTheme/Config/_config_helloworld_config_widget.html.twig #}
889+
<div class="panel panel-primary">
890+
<div class="panel-heading">
891+
<h3 class="panel-title">{{ 'mautic.config.tab.helloworld_config'|trans }}</h3>
892+
</div>
893+
<div class="panel-body">
894+
{% for field in form.children %}
895+
<div class="row">
896+
<div class="col-md-6">
897+
{{ form_row(field) }}
898+
</div>
899+
</div>
900+
{% endfor %}
901+
</div>
902+
</div>

0 commit comments

Comments
 (0)