Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 6e12e46

Browse files
authored
Merge pull request #4528 from eduard13/patch-dynamic-rows
Adding a new topic of how to add a new dynamic rows system config
2 parents 31dce1b + 490e847 commit 6e12e46

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Loading
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
![Dynamic Rows System Config]({{ site.baseurl }}/common/images/ext-best-practices/dynamic-rows-config-result.png)
72+
73+
[0]: {{ site.mage2bloburl }}/2.1/app/code/Magento/Config/Block/System/Config/Form/Field/FieldArray/AbstractFieldArray.php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.1/ext-best-practices/tutorials/dynamic-row-system-config.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.2/ext-best-practices/tutorials/dynamic-row-system-config.md

0 commit comments

Comments
 (0)