Add custom select option into system configuration using system.xml Magento 2

Written by Mahesh Makwana

Mar 24, 2021

Add custom select option into system configuration using system.xml Magento 2

We learn how to add custom select option in system configuration using system.xml file.

First, we need to create a Basic module, and after create the module add below code to your system.xml file.

For example, We will get and display all admin names into system.xml in System -> Configuration -> Dolphin Section of admin.

app/code/Dolphin/BasicModule/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="dolphin" translate="label" sortOrder="201">
            <label>Dolphin</label>
        </tab>
        <section id="basicmodule" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Basic Module</label>
            <tab>dolphin</tab>
            <resource>Dolphin_BasicModule::basicmodule_config</resource>
            <group id="genaral_setting"
                   translate="label"
                   type="text"
                   sortOrder="10"
                   showInDefault="1"
                   showInWebsite="1"
                   showInStore="1">
                <label>General Settings</label>
                <field id="admin_list"
                       translate="label"
                       type="select"
                       sortOrder="10"
                       showInDefault="1"
                       showInWebsite="1"
                       showInStore="1">
                    <label>Select Admin</label>
                    <source_model>Dolphin\BasicModule\Model\Adminhtml\System\Config\Source\AdminUser</source_model>
                    <comment><![CDATA[Comment are <b>write here</b>.]]></comment>
                </field>
            </group>
        </section>
    </system>
</config>

Here source_model tag in system.xml is defined your model PHP file, which shows your custom options. Now create AdminUser.php file.

app/code/Dolphin/BasicModule/Model/Adminhtml/System/Config/Source/AdminUser.php

<?php

namespace Dolphin\BasicModule\Model\Adminhtml\System\Config\Source;

use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;

class AdminUser implements \Magento\Framework\Option\ArrayInterface
{
    protected $_userFactory;

    public function __construct(
        UserCollectionFactory $userFactory
    ) {
        $this->_userFactory = $userFactory;
    }

    public function getOptionArray()
    {
        $adminUsers = $this->_userFactory->create();
        $options = [];
        foreach ($adminUsers as $adminUser) {
            $options[$adminUser->getId()] = __($adminUser->getUsername());
        }
        return $options;
        /*
        // you can add this way also
        $options = [];
        $options[] = __('Admin 1');
        $options[] = __('Admin 2');
        $options[] = __('Admin 3');
        return $options;
        */
    }

    public function getAllOptions()
    {
        $res = $this->getOptions();
        array_unshift($res, ['value' => '', 'label' => '']);
        return $res;
    }

    public function getOptions()
    {
        $res = [];
        foreach ($this->getOptionArray() as $index => $value) {
            $res[] = ['value' => $index, 'label' => $value];
        }
        return $res;
    }

    public function toOptionArray()
    {
        return $this->getOptions();
    }
}

Clear Cache using PHP bin/magento cache:flush and check it now you can show your custom option into your system configuration. If your want Multi-Select option then simple add type=”multiselect” to <field> tag into system.xml file.

Connect with Dolphin Web Solution for Magento 2 development.

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.

 

Mahesh Makwana

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP