10

According magento 1, we extend from "adminhtml/system_config_backend_serialized_array" to create a table like this : enter image description here

My question is : How we create it in magento 2 ?

Edit: Finally, with @Marius help : it's User-Agent Exceptions field in System=>Configuration=>General=>Design=>Design Theme.

enter image description here

We can create a new table configuration based on this field by looking at it's codes "Magento\Config\Block\System\Config\Form\Field\Regexceptions"

thienphucvx
  • 1,334
  • 2
  • 11
  • 27

2 Answers2

17

You can do it using, Company/Modulename/etc/adminhtml/system.xml

Under section -> group field

<field id="mapping" translate="label comment tooltip" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
    <label>Customer Fields Mapping</label>
    <frontend_model>Company\Modulename\Block\Adminhtml\System\Config\Form\Field\Customermap</frontend_model>
    <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
    <comment>
        <![CDATA[Add the comments!]]>
    </comment>
    <tooltip>Map the magento customer field to custom module merge_fields</tooltip>
</field>

In frontend model file inside block,

<?php
namespace Company\Modulename\Block\Adminhtml\System\Config\Form\Field;

class Customermap extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{
    /**
     * @var \Magento\Framework\Data\Form\Element\Factory
     */
    protected $_elementFactory;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Data\Form\Element\Factory $elementFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Data\Form\Element\Factory $elementFactory,
        array $data = []
    )
    {
        $this->_elementFactory  = $elementFactory;
        parent::__construct($context,$data);
    }
    protected function _construct()
    {
        $this->addColumn('field1', ['label' => __('Field1')]);
        $this->addColumn('field2', ['label' => __('FIeld2')]);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add');
        parent::_construct();
    }

}

You have display table in configuration area and after saving its value are saved inside core_config_data table.

Kishan Savaliya
  • 7,736
  • 1
  • 12
  • 27
Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
  • it's working fine. But when I save like <test> it is displaying &lt;test2&gt;. Any suggestion? – Bojjaiah Dec 23 '15 at 05:34
  • Hi, having doubt, If i wish to to add another group which consists same fields , frontend model, backend model which you have added in your system.xml then how Do I add another group with same system.xml file. while adding new group ,whether it will load the template properly ? – Jaisa Feb 12 '18 at 08:35
  • @Rakesh Jesadiya , Please have a look and answer https://magento.stackexchange.com/questions/212229/how-to-add-default-values-for-system-configuration-in-magento-2/212516#212516 – Jaisa Feb 12 '18 at 08:38
  • hi @rakesh Jesadiya, i have added fields using above code but unable to save and there is error on console field(qty) not defined. – faizanbeg Apr 26 '18 at 13:01
  • How to create the rows in this admin config table programmatically ? – Ashwani Shukla May 15 '18 at 11:37
  • Its working fine.. how to add date picker within input field – Divya Sekar Feb 18 '19 at 05:39
6

The equivalent for adminhtml/system_config_backend_serialized_array in Magento 2 is Magento\Config\Model\Config\Backend\Serialized\ArraySerialized.
You can take as example the field User-Agent Exceptions from config and try to replicate it.
The field is defined in Magento/Backend/etc/adminhtml/system.xml

Marius
  • 197,939
  • 53
  • 422
  • 830
  • Yes. It's exactly what I'm looking for. Thank you very much. – thienphucvx Dec 23 '15 at 04:28
  • hi @marius, i have added fields using code $this->addColumn('value', ['label' => __('Value')]); $this->addColumn('qty', ['label' => __('Qty')]);

    but unable to save and there is error on console field(qty) not defined Please help

    – faizanbeg Apr 27 '18 at 06:56