I need to select one value from multiple option in custom system config file.
-
Can you provide the code you have tried.– Krishna ijjadaDec 26, 2016 at 9:30
4 Answers
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')]
];
}
}
-
I got the following error: Fatal error: Cannot declare class SpedizioniList, because the name is already in use in ....– GiuseppeMar 22, 2017 at 17:31
-
-
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– GiuseppeMar 23, 2017 at 7:40
-
-
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
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')]
];
}
}
I got solution. Below line of code will work fine.
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;
}
}
\Magento\Framework\Option\ArrayInterface is deprecated instead we can use \Magento\Framework\Data\OptionSourceInterface
-
on please let me know on which version this class is deprecated? Mar 6, 2020 at 13:25
-