Magento – Magento 2 system configuration state multiselect dropdown

dynamicmagento-2.2.5statesystem.xml

First i have created country drop-down in back-end system configuration, using source model as

<source_model>Magento\Directory\Model\Config\Source\Country</source_model>

Next based on country selected, I need to populate states of that country in a multi-select field below country field.

Currently, there is option for loading state a single select drop-down. But I need it as multi-select attribute..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="new_section">
            <group id="general">
                 <field id="address_country_id" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Applicable Countries</label>
                    <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                    <frontend_class>countries</frontend_class>
                </field>
                <field id="address_region_id" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Applicable Region/State</label>
                </field>
            </group>
        </section>
    </system>
</config>

Here is the screenshot

enter image description here

Best Answer

1.If you need multi-select field at the system.xml of all country list then add the filed just like below in your group.

  <field id="address_country_id" translate="label comment" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Applicable Countries</label>
            <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
            <frontend_class>countries</frontend_class>
   </field>

2.please flush the Cache.