About PHP modifiers in UI components
What’s in this topic
This topic describes how to use PHP modifiers that are the server-side part of UI components configuration. Using modifiers is optional and might be necessary when static declaration in XML configuration files is not suitable for the tasks. For example, in cases when additional data should be loaded from database. Or the other specific example is the default product creation form, for which the modifier is a place where validations are added to display only certain fields for certain product types .
General implementation overview
DataProvider() is a PHP part of a UI component, a class responsible for the component’s data and metadata preparation. The pool of modifiers (virtual type) is injected to this data provider using the
__construct() method. The pool’s preference is defined in
So in runtime, the component structure set in the modifier is merged with the configuration that comes from the XML configuration.
Adding a custom PHP modifier
To add a PHP modifier for a UI component, take the following steps:
In your custom module, add a class that implements
\Magento\Ui\DataProvider\Modifier\ModifierInterface with the following methods:
modifyData(): for modifying UI component’s data (for example, the list of options for a select element)
modifyMeta(): for modifying UI component’s metadata (for example, name, label, description, type)
Declare your modifier in your module Di configuration
<Your_Module_dir>/etc/adminhtml/di.xml. This declaration looks like the following:
YourNamespace\YourModule\DataProvider\Modifier\Pool is a virtual class.
(If you want to use this sample in your
di.xml, replace the sample values with with the real names of your entities.)
To use your modifier, add a dependency on
\Magento\Ui\DataProvider\Modifier\PoolInterface to your UI component data provider. For illustration see