You are here

public function CurrencyForm::form in Price 8

Same name and namespace in other branches
  1. 3.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::form()
  2. 2.0.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::form()
  3. 2.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::form()
  4. 3.0.x src/Form/CurrencyForm.php \Drupal\price\Form\CurrencyForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/CurrencyForm.php, line 43

Class

CurrencyForm

Namespace

Drupal\price\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\price\Entity\CurrencyInterface $currency */
  $currency = $this->entity;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#default_value' => $currency
      ->getName(),
    '#maxlength' => 255,
    '#required' => TRUE,
  ];
  $form['currencyCode'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Currency code'),
    '#default_value' => $currency
      ->getCurrencyCode(),
    '#element_validate' => [
      '::validateCurrencyCode',
    ],
    '#pattern' => '[A-Z]{3}',
    '#placeholder' => 'XXX',
    '#maxlength' => 3,
    '#size' => 4,
    '#disabled' => !$currency
      ->isNew(),
    '#required' => TRUE,
  ];
  $iso_4217_url = Url::fromUri('https://en.wikipedia.org/wiki/ISO_4217')
    ->toString();
  $form['numericCode'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Numeric code'),
    '#description' => $this
      ->t('The three digit code, as defined by <a href=":url" target="_blank">ISO 4217</a>.', [
      ':url' => $iso_4217_url,
    ]),
    '#default_value' => $currency
      ->getNumericCode(),
    '#element_validate' => [
      '::validateNumericCode',
    ],
    '#pattern' => '[\\d]{3}',
    '#placeholder' => '999',
    '#maxlength' => 3,
    '#size' => 4,
    '#required' => TRUE,
  ];
  $form['symbol'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Symbol'),
    '#default_value' => $currency
      ->getSymbol(),
    '#maxlength' => 4,
    '#size' => 4,
    '#required' => TRUE,
  ];
  $form['fractionDigits'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Fraction digits'),
    '#description' => $this
      ->t('The number of digits after the decimal sign.'),
    '#default_value' => $currency
      ->getFractionDigits(),
    '#min' => 0,
    '#required' => TRUE,
  ];
  return $form;
}