You are here

protected function TaxNumberDefaultWidget::prepareForm in Commerce Core 8.2

Prepares the given entity form.

Ensures that the address widget refreshes the entire entity form, to allow the tax_number widget to hide itself based on the selected country.

Parameters

array $form: The entity form.

Return value

array The prepared form.

1 call to TaxNumberDefaultWidget::prepareForm()
TaxNumberDefaultWidget::formElement in modules/tax/src/Plugin/Field/FieldWidget/TaxNumberDefaultWidget.php
Returns the form for a single field widget.

File

modules/tax/src/Plugin/Field/FieldWidget/TaxNumberDefaultWidget.php, line 173

Class

TaxNumberDefaultWidget
Plugin implementation of the 'commerce_tax_number_default' widget.

Namespace

Drupal\commerce_tax\Plugin\Field\FieldWidget

Code

protected function prepareForm(array &$form) {
  if (empty($form['address']['widget'][0]['address']['#required'])) {

    // The address field is missing, optional, or using a non-standard widget.
    return $form;
  }
  $wrapper_id = Html::getUniqueId(implode('-', $form['#parents']) . '-ajax-form');
  $form += [
    '#wrapper_id' => $wrapper_id,
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
  ];
  $form['address']['widget'][0]['address']['#form_wrapper'] = $form['#wrapper_id'];
  $form['address']['widget'][0]['address']['#process'] = [
    // Keep the default #process functions defined in Address::getInfo().
    [
      Address::class,
      'processAddress',
    ],
    [
      Address::class,
      'processGroup',
    ],
    // Add our own #process.
    [
      get_class($this),
      'replaceAjaxCallback',
    ],
  ];
  return $form;
}