Commit 3646c5fa authored by a.dmitriiev's avatar a.dmitriiev Committed by bojanz

Issue #2831839 by a.dmitriiev: PriceCalculatedFormatter doesn't always load the referenced currency

parent 6eebae46
......@@ -36,6 +36,13 @@ class PriceCalculatedFormatter extends PriceDefaultFormatter implements Containe
*/
protected $chainPriceResolver;
/**
* The currency storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $currencyStorage;
/**
* The current user.
*
......@@ -74,6 +81,7 @@ class PriceCalculatedFormatter extends PriceDefaultFormatter implements Containe
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, NumberFormatterFactoryInterface $number_formatter_factory, ChainPriceResolverInterface $chain_price_resolver, StoreContextInterface $store_context, AccountInterface $current_user) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $entity_type_manager, $number_formatter_factory, $store_context);
$this->chainPriceResolver = $chain_price_resolver;
$this->currencyStorage = $entity_type_manager->getStorage('commerce_currency');
$this->currentUser = $current_user;
}
......@@ -101,12 +109,6 @@ class PriceCalculatedFormatter extends PriceDefaultFormatter implements Containe
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$currency_codes = [];
foreach ($items as $delta => $item) {
$currency_codes[] = $item->currency_code;
}
$currencies = $this->currencyStorage->loadMultiple($currency_codes);
$store = $this->storeContext->getStore();
$context = new Context($this->currentUser, $store);
$elements = [];
......@@ -116,7 +118,7 @@ class PriceCalculatedFormatter extends PriceDefaultFormatter implements Containe
$purchasable_entity = $items->getEntity();
$resolved_price = $this->chainPriceResolver->resolve($purchasable_entity, 1, $context);
$number = $resolved_price->getNumber();
$currency = $currencies[$resolved_price->getCurrencyCode()];
$currency = $this->currencyStorage->load($resolved_price->getCurrencyCode());
$elements[$delta] = [
'#markup' => $this->numberFormatter->formatCurrency($number, $currency),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment