Magento – Magento 2 – Helper function to prepare data for backend multiselect field

helpermagento2multiselect

I'm trying to use a stores allowed currencies to create a multiselect with these values for a custom module.

Now my question is, how do I best prepare the data array for the mutliselect field?

I suppose the array passed to the multiselect form has to have the following structure:

    $values = array(

        array( // option group
            'label' => 'Group 1',
            'value' =>
                array(
                    array( // Option 1
                        'value' => '1',
                        'label' => 'Option 1'
                    ),
                    array( // Option 2
                        'value' => '2',
                        'label' => 'Option 2'
                    )
                )
        ),

        array( // option group
            'label' => 'Group 2',
            'value' =>
                array(
                    array(
                        'value' => '3',
                        'label' => 'Option 3'
                    ),
                    array(
                        'value' => '4',
                        'label' => 'Option 4'
                    )
                )
        ),
    );

So for example, in my custom Class I want to return all allowed store currencies. I get the data using

$allowedCurrencies = $this->scopeConfig->getValue('currency/options/allow');

So now I need to format this into an array that can be used to create a multiselect field.

Is there a predefined helper class in Magento to prepare / format data for multiselect fields, or do I need to write this from scratch?

I know I could just output all available currencies again using the following in the system.xml

<source_model>Magento\Config\Model\Config\Source\Locale\Currency</source_model>

EDIT:

I've tried to implement David's answer below, but I have not managed. Here is my source model – as you can see I still use my own parsing hack in the getAllowedCurrencies() function.

use Magento\Framework\App\Config\ScopeConfigInterface;

/**
 * Class Config
 *
 * @package Name\MyModule\Model\Config\Backend
 */
class Config implements \Magento\Framework\Data\OptionSourceInterface
{
    /**
     * @var \Magento\Framework\App\Helper\Context
     */
    private $context;

    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(\Magento\Framework\App\Helper\Context $context)
    {
        $this->context = $context;
        $this->scopeConfig = $context->getScopeConfig();
    }

    /**
     * Get allowed currencies
     *
     * @return  array of allowed Currencies
     *
     **/
    public function getAllowedCurrencies()
    {
        $allowedCurrencies = $this->scopeConfig->getValue('currency/options/allow');
        $allowedCurrencies = explode(",", $allowedCurrencies);

        $options = [];
        foreach($allowedCurrencies as $k=>$v)
        {
            $options[] = ['value' => $v, 'label' => $v];
        }

        return $options;
    }


    /**
     * Admin Config action
     *
     * @return array
     */
    public function toOptionArray()
    {
        return $this->getAllowedCurrencies();
    }
}

Best Answer

build a Source Model for this like this one for example:

https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Model/Config/Source/Page.php

Instead of the ArrayInterface you should use the

Magento\Framework\Data\OptionSourceInterface

because of: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Option/ArrayInterface.php#L9

in that interface also the desired array structure is described

https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Data/OptionSourceInterface.php#L16