_configsFactory = $configsFactory; $this->_websiteFactory = $websiteFactory; $this->_storeFactory = $storeFactory; $this->_localeCurrency = $localeCurrency; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } /** * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function afterSave() { /** @var $collection \Magento\Config\Model\ResourceModel\Config\Data\Collection */ $collection = $this->_configsFactory->create(); $collection->addPathFilter('currency/options'); $values = explode(',', $this->getValue()); $exceptions = []; foreach ($collection as $data) { $match = false; $scopeName = __('Default scope'); if (preg_match('/(base|default)$/', $data->getPath(), $match)) { if (!in_array($data->getValue(), $values)) { $currencyName = $this->_localeCurrency->getCurrency($data->getValue())->getName(); if ($match[1] == 'base') { $fieldName = __('Base currency'); } else { $fieldName = __('Display default currency'); } switch ($data->getScope()) { case ScopeConfigInterface::SCOPE_TYPE_DEFAULT: $scopeName = __('Default scope'); break; case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE: /** @var $website \Magento\Store\Model\Website */ $website = $this->_websiteFactory->create(); $websiteName = $website->load($data->getScopeId())->getName(); $scopeName = __('website(%1) scope', $websiteName); break; case \Magento\Store\Model\ScopeInterface::SCOPE_STORE: /** @var $store \Magento\Store\Model\Store */ $store = $this->_storeFactory->create(); $storeName = $store->load($data->getScopeId())->getName(); $scopeName = __('store(%1) scope', $storeName); break; } $exceptions[] = __('Currency "%1" is used as %2 in %3.', $currencyName, $fieldName, $scopeName); } } } if ($exceptions) { throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions))); } return parent::afterSave(); } }