How to Add Select/MultiSelect Option in system.xml Configuration in Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Select and MultiSelect Option in system.xml in Magento 2.

In our previous blog we already learned how to create a system.xml configuration file.

Now in this blog we learn the below example.

  1. How to Add Select Yes/No Option.
  2. How to  Add Enable/Disable Option.
  3. How to Add Custom Select Option.
  4. How to Add Custom MultiSelect Option.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Select/MultiSelect Option in system.xml Configuration File in Magento 2.

STEPS FOR ADD SELECT/MULTISELECT OPTION IN MAGENTO 2

Step 1: Create system.xml file

app/code/Vendor/Extension/etc/adminhtml/system.xml

<?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>
        <tab id="magecurious" class="magecurious" translate="label" sortOrder="10">
            <label>Magecurious</label>
        </tab>
        <section id="extension" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Magecurious</label>
            <tab>magecurious</tab>
            <resource>Vendor_Extension::config_extension</resource>
            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Extension</label>
                 <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
                </field>
<field id="vendor_yesno" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Yes/No</label>
            <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
        </field>
<field id="vendor_select" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Select</label>
            <source_model>Vendor\Extension\Model\Config\Options</source_model>
        </field>
        <field id="vendor_multiselect" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Multi Select</label>
            <source_model>Vendor\Extension\Model\Config\Options</source_model>
        </field>
            </group>
        </section>
    </system>
</config>

Step 2: Create Options.php file

app/code/Vendor/Extension/Model/Config/Options.php

<?php

namespace Vendor\Extension\Model\Config;

use Magento\Framework\Option\ArrayInterface;
use Magento\User\Model\ResourceModel\User\CollectionFactory;

class Options implements ArrayInterface
{
    public function toOptionArray()
    {
        $options = [];
        $options[] = [
            'value' => '1',
            'label' => 'First',
        ];
        $options[] = [
            'value' => '2',
            'label' => 'Second',
        ];
         $options[] = [
            'value' => '3',
            'label' => 'Third',
        ];
        return $options;
    }
}

Step 3: Finally run the below commands

$ php bin/magento cache:clean
$ php bin/magento cache:flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Select / MultiSelect Option in the system.xml configuration file in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *