/** * {@inheritdoc} */ function getValueOptions() { if (is_null($this->valueOptions)) { $this->valueOptions = $this->formHelper->getCurrencyOptions(); } return $this->valueOptions; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $currencies = $this->configImporter->getImportableCurrencies(); if (empty($currencies)) { $form['message'] = ['#markup' => $this->t('All currencies have been imported already.')]; } else { $form['currency_code'] = ['#options' => $this->formHelper->getCurrencyOptions($currencies), '#required' => TRUE, '#title' => $this->t('Currency'), '#type' => 'select']; $form['actions'] = ['#type' => 'actions']; $form['actions']['import'] = ['#dropbutton' => 'submit', '#name' => 'import', '#type' => 'submit', '#value' => $this->t('Import')]; $form['actions']['import_edit'] = ['#dropbutton' => 'submit', '#name' => 'import_edit', '#type' => 'submit', '#value' => $this->t('Import and edit')]; } return $form; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $currency_code_from = 'XXX', $currency_code_to = 'XXX') { $plugin = $this->currencyExchangeRateProviderManager->createInstance('currency_fixed_rates'); $rate = $plugin->load($currency_code_from, $currency_code_to); $options = $this->formHelper->getCurrencyOptions(); unset($options['XXX']); $form['currency_code_from'] = array('#default_value' => $currency_code_from, '#disabled' => !is_null($rate), '#empty_value' => '', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Source currency'), '#type' => 'select'); $form['currency_code_to'] = array('#default_value' => $currency_code_to, '#disabled' => !is_null($rate), '#empty_value' => '', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Destination currency'), '#type' => 'select'); $form['rate'] = array('#limit_currency_codes' => array($currency_code_to), '#default_value' => array('amount' => !is_null($rate) ? $rate->getRate() : NULL, 'currency_code' => $currency_code_to), '#required' => TRUE, '#title' => $this->t('Exchange rate'), '#type' => 'currency_amount'); $form['actions'] = array('#type' => 'actions'); $form['actions']['save'] = array('#button_type' => 'primary', '#name' => 'save', '#type' => 'submit', '#value' => $this->t('Save')); if (!is_null($rate)) { $form['actions']['delete'] = array('#button_type' => 'danger', '#limit_validation_errors' => array(array('currency_code_from'), array('currency_code_to')), '#name' => 'delete', '#type' => 'submit', '#value' => $this->t('Delete')); } return $form; }
/** * Implements form #process callback. */ 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; }