Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortening currency list in Configuration->General (this replaces PR #19946) #20397

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = $this->getInstalledCurrencies();

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

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