5

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?

Sarfaraj Sipai
  • 976
  • 2
  • 13
  • 29
Eubie Aluad
  • 142
  • 1
  • 1
  • 13

2 Answers2

10

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

Magento2 -How to get data from system configuration

Mukesh
  • 1,428
  • 4
  • 28
  • 58
Prince Patel
  • 22,708
  • 10
  • 97
  • 119
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);
Nikolas
  • 2,281
  • 11
  • 17