You are here

public function CurrencyAmount::process in Currency 8.3

Implements form #process callback.

File

src/Element/CurrencyAmount.php, line 104

Class

CurrencyAmount
Provides an element to collect amounts of money and convert them to strings.

Namespace

Drupal\currency\Element

Code

public function process(array $element, FormStateInterface $form_state, array $form) {

  // Validate element configuration.
  if ($element['#minimum_amount'] !== FALSE && !is_numeric($element['#minimum_amount'])) {
    throw new \InvalidArgumentException('The minimum amount must be a number.');
  }
  if ($element['#maximum_amount'] !== FALSE && !is_numeric($element['#maximum_amount'])) {
    throw new \InvalidArgumentException('The maximum amount must be a number.');
  }
  if (!is_array($element['#limit_currency_codes'])) {
    throw new \InvalidArgumentException('#limit_currency_codes must be an array.');
  }
  if ($element['#limit_currency_codes'] && $element['#default_value']['currency_code'] && !in_array($element['#default_value']['currency_code'], $element['#limit_currency_codes'])) {
    throw new \InvalidArgumentException('The default currency is not in the list of allowed currencies.');
  }

  // Load the default currency.

  /** @var \Drupal\currency\Entity\CurrencyInterface $currency */
  $currency = NULL;
  if ($element['#default_value']['currency_code']) {
    $currency = $this->currencyStorage
      ->load($element['#default_value']['currency_code']);
  }
  if (!$currency) {
    $currency = $this->currencyStorage
      ->load('XXX');
  }

  // Modify the element.
  $element['#tree'] = TRUE;
  $element['#theme_wrappers'][] = 'form_element';
  $element['#attached']['library'] = [
    'currency/element.currency_amount',
  ];

  // Add the currency element.
  if (count($element['#limit_currency_codes']) == 1) {
    $element['currency_code'] = [
      '#value' => reset($element['#limit_currency_codes']),
      '#type' => 'value',
    ];
  }
  else {
    $element['currency_code'] = [
      '#default_value' => $currency
        ->id(),
      '#type' => 'select',
      '#title' => $this
        ->t('Currency'),
      '#title_display' => 'invisible',
      '#options' => $element['#limit_currency_codes'] ? array_intersect_key($this->formHelper
        ->getCurrencyOptions(), array_flip($element['#limit_currency_codes'])) : $this->formHelper
        ->getCurrencyOptions(),
      '#required' => $element['#required'],
    ];
  }

  // Add the amount element.
  $description = NULL;
  if ($element['#minimum_amount'] !== FALSE) {
    $description = $this
      ->t('The minimum amount is @amount.', [
      '@amount' => $currency
        ->formatAmount($element['#minimum_amount']),
    ]);
  }
  $element['amount'] = [
    '#default_value' => $element['#default_value']['amount'],
    '#type' => 'textfield',
    '#title' => $this
      ->t('Amount'),
    '#title_display' => 'invisible',
    '#description' => $description,
    '#prefix' => count($element['#limit_currency_codes']) == 1 ? $currency
      ->getSign() : NULL,
    '#required' => $element['#required'],
    '#size' => 9,
  ];
  return $element;
}