/**
  * {@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);
     $elements = [];
     foreach ($items as $delta => $item) {
         $currency = $currencies[$item->currency_code];
         $elements[$delta] = ['#markup' => $this->numberFormatter->formatCurrency($item->amount, $currency), '#cache' => ['contexts' => ['languages:' . LanguageInterface::TYPE_INTERFACE, 'country']]];
     }
     return $elements;
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     // Load currencies.
     $currencies = $this->currencyStorage->loadMultiple();
     $currency_codes = array_keys($currencies);
     // Stop rendering if there are no currencies available.
     if (empty($currency_codes)) {
         return $element;
     }
     $amount = $items[$delta]->amount;
     if (!empty($amount)) {
         // Convert the stored amount to the local format. For example, "9.99"
         // becomes "9,99" in many locales. This also strips any extra zeroes,
         // as configured via $this->numberFormatter->setMinimumFractionDigits().
         $amount = $this->numberFormatter->format($amount);
     }
     $element['#attached']['library'][] = 'commerce_price/admin';
     $element['#element_validate'] = [[get_class($this), 'validateElement']];
     $element['amount'] = ['#type' => 'textfield', '#title' => $element['#title'], '#default_value' => $amount, '#required' => $element['#required'], '#size' => 10, '#maxlength' => 255, '#placeholder' => $this->numberFormatter->format('9.99')];
     if (count($currency_codes) == 1) {
         $last_visible_element = 'amount';
         $currency_code = reset($currency_codes);
         $element['amount']['#field_suffix'] = $currency_code;
         $element['currency_code'] = ['#type' => 'value', '#value' => $currency_code];
     } else {
         $last_visible_element = 'currency_code';
         $element['currency_code'] = ['#type' => 'select', '#title' => $this->t('Currency'), '#default_value' => $items[$delta]->currency_code, '#options' => array_combine($currency_codes, $currency_codes), '#title_display' => 'invisible', '#field_suffix' => ''];
     }
     // Add the help text if specified.
     // Replicates the commerce_field_widget_form_alter() logic because
     // the copied help text can't be reached by the alter.
     $base_field = $this->fieldDefinition instanceof BaseFieldDefinition;
     $has_override = $this->fieldDefinition->getSetting('display_description');
     $hide_description = $base_field && !$has_override;
     if (!empty($element['#description']) && !$hide_description) {
         $element[$last_visible_element]['#field_suffix'] .= '<div class="description">' . $element['#description'] . '</div>';
     }
     return $element;
 }