Skip to content

Commit

Permalink
Shortening currency list in Configuration->General
Browse files Browse the repository at this point in the history
use isset

array_flip
  • Loading branch information
melaxon authored and amol2jcommerce committed Apr 30, 2019
1 parent 1e230b1 commit f54c202
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions app/code/Magento/Config/Model/Config/Source/Locale/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
* See COPYING.txt for license details.
*/

/**
* Locale currency source
*/
namespace Magento\Config\Model\Config\Source\Locale;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\ListsInterface;

/**
* Locale currency source.
*
* @api
* @since 100.0.2
*/
Expand All @@ -21,27 +24,70 @@ class Currency implements \Magento\Framework\Option\ArrayInterface
protected $_options;

/**
* @var \Magento\Framework\Locale\ListsInterface
* @var ListsInterface
*/
protected $_localeLists;

/**
* @param \Magento\Framework\Locale\ListsInterface $localeLists
* @var ScopeConfigInterface
*/
public function __construct(\Magento\Framework\Locale\ListsInterface $localeLists)
{
private $config;

/**
* @var array
*/
private $installedCurrencies;

/**
* @param ListsInterface $localeLists
* @param ScopeConfigInterface $config
*/
public function __construct(
ListsInterface $localeLists,
ScopeConfigInterface $config = null
) {
$this->_localeLists = $localeLists;
$this->config = $config ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
}

/**
* @return array
* @inheritdoc
*/
public function toOptionArray()
{
if (!$this->_options) {
$this->_options = $this->_localeLists->getOptionCurrencies();
}
$options = $this->_options;

$selected = array_flip($this->getInstalledCurrencies());

$options = array_filter(
$this->_options,
function ($option) use ($selected) {
return isset($selected[$option['value']]);
}
);

return $options;
}

/**
* Retrieve Installed Currencies.
*
* @return array
*/
private function getInstalledCurrencies()
{
if (!$this->installedCurrencies) {
$this->installedCurrencies = explode(
',',
$this->config->getValue(
'system/currency/installed',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
);
}

return $this->installedCurrencies;
}
}

0 comments on commit f54c202

Please sign in to comment.