10

I need to select one value from multiple options using a drop down select list in a custom system config file.

How should I write the config file?

1
  • Can you provide the code you have tried. Dec 26, 2016 at 9:30

4 Answers 4

25

Try this code.

system.xml

<field id="list_mode" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
   <label>List Mode</label>        
   <source_model>Vendor\Module\Model\Config\Source\ListMode</source_model>
</field>

Vendor\Module\Model\Config\Source\ListMode.php

namespace Vendor\Module\Model\Config\Source;

class ListMode implements \Magento\Framework\Data\OptionSourceInterface
{
 public function toOptionArray()
 {
  return [
    ['value' => 'grid', 'label' => __('Grid Only')],
    ['value' => 'list', 'label' => __('List Only')],
    ['value' => 'grid-list', 'label' => __('Grid (default) / List')],
    ['value' => 'list-grid', 'label' => __('List (default) / Grid')]
  ];
 }
}
5
  • I got the following error: Fatal error: Cannot declare class SpedizioniList, because the name is already in use in ....
    – Giuseppe
    Mar 22, 2017 at 17:31
  • @Giuseppe ask a question and write your code. Mar 23, 2017 at 4:54
  • there is no need to ask a duplicate question ;), for M2 v.2.1.1 your code works perfectly by adding the namespace Vendor\Module\Model\Config\Source; before the class declaration. Hope may helps other developer. thank you
    – Giuseppe
    Mar 23, 2017 at 7:40
  • @Giuseppe Great. Mar 23, 2017 at 7:49
  • I'm facing following error: Element 'source_model': [facet 'pattern'] The value 'Vendor\Module\Model\Config\Source \Shippingmethods' is not accepted by the pattern '[A-Za-z0-9_\\:]+'. Apr 5, 2020 at 9:29
7

As Magento\Framework\Option\ArrayInterface has been deprecated since v.102.0.1, I'm posting it in the current standard adopted by Magento (I'm using 2.3.3 and PHP 7.3):

<?php

namespace Vendor\Package\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * Class Profile
 * @package Vendor\Package\Model\Config\Source
 */
class Profile implements OptionSourceInterface
{
    /**
     * @return array
     */
    public function toOptionArray() : array
    {
        return [
            ['value' => '', 'label' => __('-- Select an Option --')],
            ['value' => 'grid', 'label' => __('Grid Only')],
            ['value' => 'list', 'label' => __('List Only')],
            ['value' => 'grid-list', 'label' => __('Grid (default) / List')],
            ['value' => 'list-grid', 'label' => __('List (default) / Grid')]
        ];
    }
}
4

Use these files:

system.xml

<field id="select" translate="label" type="select"
       sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Select Option</label>
    <source_model>Vendor\Modulename\Model\Config\Source\Products</source_model>
</field>

Model file

Vendor\Modulename\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class Products implements ArrayInterface
{
    /*
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
        $arr = $this->toArray();
        $ret = [];
        foreach ($arr as $key => $value) {
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        $choose = [
            '1' => 'A',
            '2' => 'B',
            '3' => 'C',
            '4' => 'D'
            
        ];
        return $choose;
    }
}
1

\Magento\Framework\Option\ArrayInterface is deprecated instead we can use \Magento\Framework\Data\OptionSourceInterface

2
  • on please let me know on which version this class is deprecated? Mar 6, 2020 at 13:25
  • Hi @renu did u worked in kensium? Mar 6, 2020 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.