I have created a system.xml file with a multiselect type field somewhere in my admin system config. I just want to know on how to retrieve the selected values in that field?
-
I found the solution by using Mage::getStoreConfig(section_group_field path) – Eubie Aluad Oct 24 '17 at 09:41
-
https://bit.ly/2w7H6y2 using link got all the multiselect attribute. – Rakesh Jesadiya Aug 23 '18 at 17:41
2 Answers
Define <source_model> in system.xml:
<field id="Moduleposition" translate="Module" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Module Position</label>
<source_model>Vendor\Module\Model\Config\Source\ConfigOption</source_model>
</field>
Now create ConfigOption.php:
<?php
namespace Vendor\Module\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class ConfigOption implements OptionSourceInterface
{
public function toOptionArray()
{
return [
['value' => '1', 'label' => __('Top Right')],
['value' => '2', 'label' => __('Top Left')],
['value' => '3', 'label' => __('Middle Right')],
['value' => '4', 'label' => __('Middle')],
['value' => '5', 'label' => __('Middle Left')],
['value' => '6', 'label' => __('Bottom Right')],
['value' => '7', 'label' => __('Bottom Left')]
];
}
}
You will get multiselect value in string with value1,value2,value3 format.
You can use explode() function to convert it into array like this
$value = explode(',', $multiSelectValue);
For more info about how you can get system.xml config value refer this question
- 1,428
- 4
- 28
- 58
- 22,708
- 10
- 97
- 119
Just like all the other values from your system.xml. The result will be a string with comma separated values. So you have explode the string and have an array for better handling like
$miltiselectValues = explode(',', $configValue);
- 2,281
- 11
- 17