How To

How to add dynamic-row multi select in system configuration in Magento 2?

Hello, Magento pals, 

I hope you are doing great and had a splendid first weekend of 2020. I am back with another interesting and exciting article on Magento 2. Last time, we learned how you can Show Minicart on Custom Popup. Today, we are going to learn how you can add dynamic-row multi-select in system configuration for Magento 2. 

The system configuration is the heart of any extension from where the store owner can handle every functionalities and feature. It enables the Magento developer to scale and meet customer’s requirements. 

Till date, we have written several blogs regarding adding different configuration fields in our MageComp Blog. But many times, you came across the requirement of capturing multiple values irrespective of the number of values, at that time we need to use the dynamic field inside backend configuration so the admin can add multiple values using the backend.

To have this facility enabled on your Magento 2 store, we have developed codes. These codes will mitigate the function of adding dynamic-row multiselect in the configuration. 

Now, follow the below steps to add dynamic-row multi-select in system configuration:

Step 1: First, we need to create a “Registration.php” file inside our extension on this path.
app\code\Vendor\Extension

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Extension',
    __DIR__
);

Step 2: After that, we need to create “Module.xml” file inside extension etc folder
app\code\Vendor\Extension\etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0" />
</config>

Step 3: After that, we need to create a “routes.xml” file in the same etc folder.
app\code\Vendor\Extension\etc\adminhtml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="extension" frontName="extension">
            <module name="Vendor_Extension" />
        </route>
    </router>
</config>
 

Step 4: Now, we have to create one more file “system.xml” inside the same etc folder.
app\code\Vendor\Extension\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
      <tab id="extension" translate="label" sortOrder="100">
         <label>My Extension Configuration</label>
      </tab>
      <section id="extension" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
         <label>Add Dynamic Row Multiselect</label>
         <tab>extension</tab>
         <resource>Vendor_Extension::extension</resource>
         <group id="quantity_ranges" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label> Add Dynamic Row Multiselect </label>
            <field id="ranges" translate="label" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
               <label>Ranges</label>
               <frontend_model>Vendor\Extension\Block\Adminhtml\Form\Field\Ranges</frontend_model>
               <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
            </field>
         </group>
      </section>
    </system>
</config>

Step 5: Now, we have to create one more file “Ranges.php” inside the Block folder. app\code\Vendor\Extension\Block\Adminhtml\Form\Field

<?php
namespace Vendor\Extension\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;

class Ranges extends AbstractFieldArray
{

    private $taxRenderer;
   
    protected function _prepareToRender()
    {
        $this->addColumn('price', ['label' => __('Price'), 'class' => 'required-entry']);
        
        $this->addColumn('custom', [
            'label' => __('Country'),
            'renderer' => $this->getCountryRenderer(),
            'extra_params' => 'multiple="multiple"'
        ]);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add');
    }

    protected function _prepareArrayRow(DataObject $row)
    {
        $options = [];

        $tax = $row->getTax();
        if ($tax !== null) {
            $options['option_' . $this->getTaxRenderer()->calcOptionHash($tax)] = 'selected="selected"';
        }

        $countries = $row->getCountry();
        if (count($countries) > 0) {
            foreach ($countries as $country) {
                $options['option_' . $this->getCountryRenderer()->calcOptionHash($country)]
                    = 'selected="selected"';
            }
        }

        $row->setData('option_extra_attrs', $options);
    }

    private function getCountryRenderer()
    {
            $this->countryRenderer = $this->getLayout()->createBlock(
                \Vendor\Extension\Block\Adminhtml\Form\Field\CountryColumn::class,
                '',
                ['data' => ['is_render_to_js_template' => true]]
            );
        return $this->countryRenderer;
    }
}
 

Step 6: Lastly, Create “CountryColumn.php” inside the same Block folder.
app\code\Vendor\Extension\Block\Adminhtml\Form\Field

<?php
declare(strict_types=1);
namespace Vendor\Extension\Block\Adminhtml\Form\Field;

use Magento\Braintree\Helper\Country;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;

class CountryColumn extends Select
{
    private $countryHelper;

    public function __construct(
        Context $context,
        Country $countryHelper,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->countryHelper = $countryHelper;
    }

    public function setInputName($value)
    {
        return $this->setName($value . '[]');
    }

    public function _toHtml(): string
    {
        if (!$this->getOptions()) {
            $this->setOptions($this->countryHelper->getCountries());
        }
        $this->setExtraParams('multiple="multiple"');
        return parent::_toHtml();
    }
}

That’s it for today! You have successfully Implemented add dynamic-row multi-select in system configuration for Magento 2, and you are free to manipulate this code according to your development needs.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and let us know if you are facing any issue while implementing this code at our support desk. We will be happy to help. 

Happy reading!

Click to rate this post!
[Total: 14 Average: 4.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago