Magento

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

Written by Mahesh Makwana

Mar 24

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.

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

Written by Mahesh Makwana

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP