-
Notifications
You must be signed in to change notification settings - Fork 38
docs: updated documentation for Configuration component. #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 5.x
Are you sure you want to change the base?
Changes from 2 commits
8f00a09
1f70453
c0996a2
af8f4a2
6596e08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -699,4 +699,215 @@ | |||||
|
||||||
.. 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``. | ||||||
|
||||||
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information. | ||||||
.. note:: Parameters aren't exposed to the UI by default. See :ref:`components/config:Configuration` for more information. | ||||||
Check warning on line 702 in docs/plugins/config.rst
|
||||||
|
||||||
|
||||||
Custom config parameters | ||||||
************************ | ||||||
You can define custom configuration parameters in your plugin to support configurable features, such as enabling or disabling functionality. | ||||||
Check warning on line 707 in docs/plugins/config.rst
|
||||||
|
||||||
Mautic plugins allow you to define these parameters for use within your plugin’s code. Store these parameters in ``app/config/local.php``, and define their default values in the plugin’s own config file to ensure stability and avoid errors. | ||||||
Check warning on line 709 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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 `plugin’s config file <https://devdocs.mautic.org/en/latest/plugins/config.html#parameters-config-items>`_. This guarantees that the parameter exists and has a fallback value. | ||||||
Check failure on line 711 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
To add these configuration options in the configuration page, you’ll need: | ||||||
Check warning on line 713 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
- An `event subscriber <https://devdocs.mautic.org/en/latest/plugins/event_listeners.html>`_ to register the configuration. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
- A `form type <https://devdocs.mautic.org/en/latest/components/forms.html>`_ that defines the fields. | ||||||
Check warning on line 716 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
- A specific view for rendering the form. | ||||||
Check warning on line 717 in docs/plugins/config.rst
|
||||||
|
||||||
.. note:: | ||||||
|
||||||
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. | ||||||
Check failure on line 721 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
Config event subscriber | ||||||
Check failure on line 724 in docs/plugins/config.rst
|
||||||
======================= | ||||||
|
||||||
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``. | ||||||
Check failure on line 727 in docs/plugins/config.rst
|
||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The following code example demonstrates how the event subscriber is structured in a plugin. | ||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
<?php | ||||||
// plugins/HelloWorldBundle/EventListener/ConfigSubscriber.php | ||||||
|
||||||
namespace MauticPlugin\HelloWorldBundle\EventListener; | ||||||
|
||||||
use Mautic\ConfigBundle\Event\ConfigEvent; | ||||||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||||||
use Mautic\ConfigBundle\ConfigEvents; | ||||||
use Mautic\ConfigBundle\Event\ConfigBuilderEvent; | ||||||
|
||||||
/** | ||||||
* Class ConfigSubscriber | ||||||
*/ | ||||||
class ConfigSubscriber extends CommonSubscriber | ||||||
{ | ||||||
/** | ||||||
* @return array | ||||||
*/ | ||||||
static public function getSubscribedEvents() | ||||||
{ | ||||||
return [ | ||||||
ConfigEvents::CONFIG_ON_GENERATE => ['onConfigGenerate', 0], | ||||||
ConfigEvents::CONFIG_PRE_SAVE => ['onConfigSave', 0] | ||||||
]; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param ConfigBuilderEvent $event | ||||||
*/ | ||||||
public function onConfigGenerate(ConfigBuilderEvent $event) | ||||||
{ | ||||||
$event->addForm( | ||||||
[ | ||||||
'formAlias' => 'helloworld_config', | ||||||
'formTheme' => 'HelloWorldBundle:FormTheme\Config', | ||||||
'parameters' => $event->getParametersFromConfig('HelloWorldBundle') | ||||||
] | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @param ConfigEvent $event | ||||||
*/ | ||||||
public function onConfigSave(ConfigEvent $event) | ||||||
{ | ||||||
/** @var array $values */ | ||||||
$values = $event->getConfig(); | ||||||
|
||||||
// Manipulate the values | ||||||
if (!empty($values['helloworld_config']['custom_config_option'])) { | ||||||
$values['helloworld_config']['custom_config_option'] = htmlspecialchars($values['helloworld_config']['custom_config_option']); | ||||||
} | ||||||
|
||||||
// Set updated values | ||||||
$event->setConfig($values); | ||||||
} | ||||||
} | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
|
||||||
Subscribed events | ||||||
----------------- | ||||||
|
||||||
The event subscriber listens to the following events: | ||||||
|
||||||
- ``ConfigEvents::CONFIG_ON_GENERATE``: | ||||||
This event is dispatched when the configuration form is built. This allows the plugin to inject its own tab and configuration options. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
- ``ConfigEvents::CONFIG_PRE_SAVE``: | ||||||
This event is triggered before the form values are rendered and saved to the ``local.php`` file. This allows the plugin to clean up or modify the data before writing it to ``local.php``. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Generate plugin configuration | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
--------------------------------- | ||||||
|
||||||
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: | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. list-table:: | ||||||
:header-rows: 1 | ||||||
|
||||||
* - Key | ||||||
- Description | ||||||
* - ``formAlias`` | ||||||
- The alias of the form type class that defines the expected form elements. | ||||||
* - ``formTheme`` | ||||||
- The view that formats the configuration form elements, e.g., ``HelloWorldBundle:FormTheme\Config``. | ||||||
* - ``parameters`` | ||||||
- An array of custom configuration elements. ``Use $event->getParametersFromConfig('HelloWorldBundle')`` to retrieve them from the plugin’s configuration file. | ||||||
|
||||||
Modify configuration before saving | ||||||
-------------------------------------- | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Register the event subscriber | ||||||
-------------------------- | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Register the subscriber 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please also fix the link. Use the sphinx link reference, we don't link like this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this look?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you build the docs with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it does😊. |
||||||
|
||||||
|
||||||
Config form | ||||||
============= | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The form type is used to generate the form fields in the main configuration form. See the `Forms documentation <https://devdocs.mautic.org/en/latest/components/forms.html>`_ for more information about using form types. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please also fix the link. See earlier comments. |
||||||
|
||||||
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>`_ | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
. | ||||||
|
||||||
Below is an example of a form type class that adds a custom configuration option to the plugin's configuration form. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
<?php | ||||||
// plugins/HelloWorldBundle/Form/Type/ConfigType.php | ||||||
|
||||||
namespace MauticPlugin\HelloWorldBundle\Form\Type; | ||||||
|
||||||
use Symfony\Component\Form\AbstractType; | ||||||
use Symfony\Component\Form\FormBuilderInterface; | ||||||
|
||||||
/** | ||||||
* Class ConfigType | ||||||
*/ | ||||||
class ConfigType extends AbstractType | ||||||
{ | ||||||
/** | ||||||
* @param FormBuilderInterface $builder | ||||||
* @param array $options | ||||||
*/ | ||||||
public function buildForm(FormBuilderInterface $builder, array $options) | ||||||
{ | ||||||
$builder->add( | ||||||
'custom_config_option', | ||||||
'text', | ||||||
array( | ||||||
'label' => 'plugin.helloworld.config.custom_config_option', | ||||||
'data' => $options['data']['custom_config_option'], | ||||||
'attr' => array( | ||||||
'tooltip' => 'plugin.helloworld.config.custom_config_option_tooltip' | ||||||
) | ||||||
) | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* {@inheritdoc} | ||||||
*/ | ||||||
public function getName() | ||||||
{ | ||||||
return 'helloworld_config'; | ||||||
} | ||||||
} | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Config template | ||||||
============= | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
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. | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The template should be structured in a panel format to match the rest of the configuration UI. | ||||||
|
||||||
Below is an example of how the template should be structured: | ||||||
|
||||||
.. code-block:: php | ||||||
|
||||||
<?php | ||||||
// plugins/HelloWorldBundle/Views/FormTheme/Config/_config_helloworld_config_widget.html.php | ||||||
|
||||||
?> | ||||||
|
||||||
<div class="panel panel-primary"> | ||||||
<div class="panel-heading"> | ||||||
<h3 class="panel-title"><?php echo $view['translator']->trans('mautic.config.tab.helloworld_config'); ?></h3> | ||||||
</div> | ||||||
<div class="panel-body"> | ||||||
<?php foreach ($form->children as $f): ?> | ||||||
<div class="row"> | ||||||
<div class="col-md-6"> | ||||||
<?php echo $view['form']->row($f); ?> | ||||||
</div> | ||||||
</div> | ||||||
<?php endforeach; ?> | ||||||
</div> | ||||||
</div> | ||||||
Teebarh marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.