Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $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'
- )
- )
- ),
- );
- $allowedCurrencies = $this->scopeConfig->getValue('currency/options/allow');
- <source_model>MagentoConfigModelConfigSourceLocaleCurrency</source_model>
- use MagentoFrameworkAppConfigScopeConfigInterface;
- /**
- * Class Config
- *
- * @package NameMyModuleModelConfigBackend
- */
- class Config implements MagentoFrameworkDataOptionSourceInterface
- {
- /**
- * @var MagentoFrameworkAppHelperContext
- */
- private $context;
- /**
- * @var ScopeConfigInterface
- */
- private $scopeConfig;
- /**
- * @param MagentoFrameworkAppHelperContext $context
- */
- public function __construct(MagentoFrameworkAppHelperContext $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();
- }
- }
- <field id="currency" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="10" translate="label comment" type="multiselect">
- <label>Currency</label>
- <source_model>NamespaceModulenameModelConfigSourceCurrency</source_model>
- </field>
- <?php
- namespace NamespaceModulenameModelConfigSource;
- use MagentoFrameworkOptionArrayInterface;
- class Currency implements ArrayInterface {
- /**
- * @var CollectionFactory
- */
- public $currency;
- /**
- * @param MagentoCurrencySymbolModelSystemCurrencysymbol $currency
- */
- public function __construct(
- MagentoCurrencySymbolModelSystemCurrencysymbol $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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement