1

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 9:41
  • bit.ly/2w7H6y2 using link got all the multiselect attribute. – Rakesh Jesadiya Aug 23 '18 at 17:41
2

Define source model in system.xml

<field id="Moduleposition" translate="Module" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
    <Module>Module Position</Module>
    <source_model>Vendor\Module\Model\Config\Source\ConfigOption</source_model>
</field>

Now create ConfigOption.php at

<?php

namespace Vendor\Module\Model\Config\Source;

class ConfigOption implements \Magento\Framework\Option\ArrayInterface
{
    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

Magento2 -How to get data from system configuration

  • I will note this sire! – Eubie Aluad Oct 24 '17 at 9:43
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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