You are here

public function ExchangerProviderBase::buildConfigurationForm in Commerce Exchanger 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/Commerce/ExchangerProvider/ExchangerProviderBase.php, line 162

Class

ExchangerProviderBase
Base class for Commerce exchanger provider plugins.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $definition = $this
    ->getPluginDefinition();

  // Skip everything for manual plugin.
  if (empty($definition['manual'])) {
    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => t('Api Key'),
      '#description' => t('Api key'),
      '#default_value' => $this->configuration['api_key'] ?? NULL,
      '#required' => $definition['api_key'] ?? FALSE,
      '#access' => $definition['api_key'] ?? FALSE,
    ];
    $auth = $definition['auth'] ?? FALSE;
    $form['auth'] = [
      '#type' => 'details',
      '#title' => t('Authentication'),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#access' => $auth,
    ];
    $form['auth']['username'] = [
      '#type' => 'textfield',
      '#title' => t('Username'),
      '#description' => t('Api key'),
      '#default_value' => $this->configuration['auth']['username'] ?? NULL,
      '#required' => $auth,
      '#access' => $auth,
    ];
    $form['auth']['password'] = [
      '#type' => 'textfield',
      '#title' => t('Password'),
      '#description' => t('Api key'),
      '#default_value' => $this->configuration['auth']['password'] ?? NULL,
      '#required' => $auth,
      '#access' => $auth,
    ];
    $enterprise = empty($definition['base_currency']) ? $this->configuration['enterprise'] : FALSE;
    $form['enterprise'] = [
      '#type' => 'checkbox',
      '#default_value' => $enterprise,
      '#title' => t('Enterprise'),
      '#description' => t('If provider supports querying per multiple base currencies, not just single base currency'),
      '#disabled' => !empty($definition['base_currency']),
    ];
    $cross_sync = !empty($definition['base_currency']) ? TRUE : $this->configuration['use_cross_sync'] ?? FALSE;
    $form['use_cross_sync'] = [
      '#type' => 'checkbox',
      '#default_value' => $cross_sync,
      '#title' => t('Use cross conversion between non default currencies.'),
      '#description' => t('If enabled only the rates between the default currency and the other currencies have to be managed. The rates between the other currencies is derived from their rates relative to the default currency.'),
      '#disabled' => !empty($definition['base_currency']),
    ];
    $currencies = $this->currencies;
    $form['base_currency'] = [
      '#type' => 'select',
      '#title' => t('Base currency'),
      '#description' => t('Select base currency upon all others are calculated, if your providers does not support Enterprise mode'),
      '#options' => $currencies,
      '#default_value' => $definition['base_currency'] ?? $this->configuration['base_currency'],
      '#required' => !empty($this->configuration['enterprise']),
      '#access' => empty($this->configuration['enterprise']),
      '#disabled' => !empty($definition['base_currency']),
    ];
    $form['cron'] = [
      '#type' => 'select',
      '#title' => t('Exchange rates cron'),
      '#description' => t('Select how often exchange rates should be imported. Note about EBC, they update exchange rates once a day'),
      '#options' => [
        1 => t('Once a day'),
        2 => t('12 hours'),
        3 => t('8 hours'),
        4 => t('6 hours'),
      ],
      '#default_value' => $definition['refresh_once'] ?? $this->configuration['cron'],
      '#disabled' => $definition['refresh_once'],
    ];
  }

  // Check if supporting different modes (test, live).
  if ($this
    ->supportingModes()) {
    $form['mode'] = [
      '#type' => 'radios',
      '#title' => t('Mode'),
      '#options' => [
        'live' => t('Live'),
        'test' => t('Test'),
      ],
      '#default_value' => $this->configuration['mode'],
      '#required' => TRUE,
    ];
  }
  else {
    $form['mode'] = [
      '#type' => 'value',
      '#value' => 'live',
    ];
  }
  $form['demo_amount'] = [
    '#type' => 'textfield',
    '#title' => t('Amount for example conversion:'),
    '#size' => 10,
    '#default_value' => $this->configuration['demo_amount'] ?? 100,
  ];
  return $form;
}