|
| 1 | +--- |
| 2 | +group: extension-best-practices |
| 3 | +subgroup: 02_Extension-Coding |
| 4 | +title: Creating a dynamic row system config |
| 5 | +menu_title: Creating a dynamic row system config |
| 6 | +menu_order: 1010 |
| 7 | +functional_areas: |
| 8 | + - Standards |
| 9 | +--- |
| 10 | + |
| 11 | +This tutorial shows you how to add a new dynamic rows system configuration in the {% glossarytooltip 18b930cf-09cc-47c9-a5e5-905f86c43f81 %}Magento admin{% endglossarytooltip %}, by extending the [Magento/Config/Block/System/Config/Form/Field/FieldArray/AbstractFieldArray][0]{:target="_blank"} class. |
| 12 | + |
| 13 | +## Step 1: Add a new system field |
| 14 | + |
| 15 | +> `etc/adminhtml/system.xml` |
| 16 | +
|
| 17 | +```xml |
| 18 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> |
| 19 | + <system> |
| 20 | + <section id="general" translate="label" type="text"> |
| 21 | + <group id="quantity_ranges" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> |
| 22 | + <label>Quantity Ranges</label> |
| 23 | + <field id="ranges" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1"> |
| 24 | + <label>Ranges</label> |
| 25 | + <frontend_model>[vendor]\[module]\Block\Adminhtml\Form\Field\Ranges</frontend_model> |
| 26 | + <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model> |
| 27 | + </field> |
| 28 | + </group> |
| 29 | + </section> |
| 30 | + </system> |
| 31 | +</config> |
| 32 | +``` |
| 33 | + |
| 34 | +This code adds a new system config in **STORES** > Settings > **Configuration** > GENERAL > **General** > **Quantity Ranges**. |
| 35 | + |
| 36 | +## Step 2: Create the front-end model class |
| 37 | + |
| 38 | +> `Block/Adminhtml/Form/Field/Ranges.php` |
| 39 | +
|
| 40 | +```php |
| 41 | +<?php |
| 42 | +namespace Learning\GreetingMessage\Block\Adminhtml\Form\Field; |
| 43 | + |
| 44 | +use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; |
| 45 | + |
| 46 | +/** |
| 47 | + * Class Ranges |
| 48 | + */ |
| 49 | +class Ranges extends AbstractFieldArray |
| 50 | +{ |
| 51 | + /** |
| 52 | + * Prepare rendering the new field by adding all the needed columns |
| 53 | + */ |
| 54 | + protected function _prepareToRender() |
| 55 | + { |
| 56 | + $this->addColumn('from_qty', ['label' => __('From'), 'class' => 'required-entry']); |
| 57 | + $this->addColumn('to_qty', ['label' => __('To'), 'class' => 'required-entry']); |
| 58 | + $this->addColumn('price', ['label' => __('Price'), 'class' => 'required-entry']); |
| 59 | + $this->_addAfter = false; |
| 60 | + $this->_addButtonLabel = __('Add'); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +This block prepares the desired columns for inclusion in the new config. |
| 66 | + |
| 67 | +## Result |
| 68 | + |
| 69 | +The result is a new dynamic system row field in the Admin panel. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +[0]: {{ site.mage2bloburl }}/2.1/app/code/Magento/Config/Block/System/Config/Form/Field/FieldArray/AbstractFieldArray.php |
0 commit comments