Sometimes module requires to add some custom control elements into the configuration section, like shown on the image below:
Here is how it’s possible to acheive in Magento 2. We need to create the element <frontend_model> for the required attribute in the file app/MyCompany/MyModyle/etc/adminhtml/system.xml. This tag will define our custom field. The block should be extended from the class \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
1 2 3 |
<field id="customfields" translate="label" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Customer Fields</label> <frontend_model>MyCompany\MyModule\Block\Customerfield</frontend_model> </field> |
Magento Webdesign
Take your online store to the next level with BelVG Magento Webdesign
Visit the pageIn order to customize the field we should update the _construct() method of the block MyCompany\MyModule\Block\Customerfield:
1 2 3 4 5 6 7 8 |
protected function _construct() { $this->addColumn('name', ['label' => __('Name')]); $this->addColumn('value', ['label' => __('Value')]); $this->_addAfter = false; $this->_addButtonLabel = __('Add'); parent::_construct(); } |
Once the fields have been added, you will get the following form:
But rendering is only the first step. To save the form, we need to collect user input and send it to the backend. Here we should use the Magento\Config\Model\Config\Backend\Serialized\ArraySerialized class to serialize data before saving.
Also we should specify the <backend_model> class to be used for processing and saving serialized data. Backend model is also defined in the system.xml file.
1 2 3 4 5 |
<field id="customfields" translate="label" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0"> <label>Customer Fields</label> <frontend_model>MyCompany\MyModule\Block\Customerfield</frontend_model> <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model> </field> |
So now the data of the form will be serialized when saved.
To use the form data in the module we need to implement the unserialization task:
1 |
$fields = unserialize($this->_scopeConfig->getValue(self::PATH_TO_FIELD)); |
This way the array of the form data is transmitted into the fields variable.
In this example I have described only the simplest way of customizing fields. But there are also other more interesting ways that let you set up and display different fields. Let’s check the method Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray::addColumn(), that is used for adding new fields:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function addColumn($name, $params) { $this->_columns[$name] = [ 'label' => $this->_getParam($params, 'label', 'Column'), 'size' => $this->_getParam($params, 'size', false), 'style' => $this->_getParam($params, 'style'), 'class' => $this->_getParam($params, 'class'), 'renderer' => false, ]; if (!empty($params['renderer']) && $params['renderer'] instanceof \Magento\Framework\View\Element\AbstractBlock) { $this->_columns[$name]['renderer'] = $params['renderer']; } } |
As seen from the method, it is possible not only to define the style but also the size of fields and even the use of your own blocks for rendering.
As you see, Magento 2 offers a very convenient and flexible tool for creation and changing form elements in the system configuration section.
Magento Extensions
Take your online store to the next level with BelVG Magento Extensions
Visit the page