0

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

2 Answers 2

0

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

2
  • Thanks David, this seems like the way to do it, but I have not managed. I have posted my Source Model above, can you tell how I need to edit it to get it work as I just keep getting a blank page if I try to implement it as explained above. Commented Feb 10, 2016 at 10:52
  • do you have the developer mode enabled? blank page could mean many things, you need an actual error message. Have you cleaned your var/generation folder? Commented Feb 10, 2016 at 11:37
0

Here is a better way

Add your field in system

<field id="currency" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="10" translate="label comment" type="multiselect"> <label>Currency</label> <source_model>Namespace\Modulename\Model\Config\Source\Currency</source_model> </field> 

Your Source Model File

<?php namespace Namespace\Modulename\Model\Config\Source; use Magento\Framework\Option\ArrayInterface; class Currency implements ArrayInterface { /** * @var CollectionFactory */ public $currency; /** * @param \Magento\CurrencySymbol\Model\System\Currencysymbol $currency */ public function __construct( \Magento\CurrencySymbol\Model\System\Currencysymbol $currency ) { $this->currency = $currency; } /** * Return currency symbol properties array based on config values * * @return array */ protected function getCurrency() { return $this->currency->getCurrencySymbolsData(); } /** * @return array */ public function toOptionArray() { $currency = $this->getCurrency(); $options = []; foreach ($this->getCurrency() as $code => $currency) { $options[$code] = [ 'label' => $currency['displayName'], 'value' => $code ]; } return $options; } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.