You are here

public static function ZoneTerritory::processTerritory in Address 8

Processes the zone territory form element.

Parameters

array $element: The form element to process.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The processed element.

Throws

\InvalidArgumentException Thrown when #available_countries or #used_fields is malformed.

File

src/Element/ZoneTerritory.php, line 125

Class

ZoneTerritory
Provides a zone territory form element.

Namespace

Drupal\address\Element

Code

public static function processTerritory(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $id_prefix = implode('-', $element['#parents']);
  $wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper');

  // The #value has the new values on #ajax, the #default_value otherwise.
  $value = $element['#value'];
  $element = [
    '#tree' => TRUE,
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
    // Pass the id along to other methods.
    '#wrapper_id' => $wrapper_id,
  ] + $element;
  $element['country_code'] = [
    '#type' => 'address_country',
    '#title' => t('Country'),
    '#available_countries' => $element['#available_countries'],
    '#default_value' => $element['#default_value']['country_code'],
    '#required' => $element['#required'],
    '#limit_validation_errors' => [],
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'ajaxRefresh',
      ],
      'wrapper' => $wrapper_id,
    ],
    '#weight' => -100,
  ];
  if (!$element['#required']) {
    $element['country_code']['#empty_value'] = '';
  }
  if (!empty($value['country_code'])) {

    /** @var \CommerceGuys\Addressing\AddressFormat\AddressFormat $address_format */
    $address_format = \Drupal::service('address.address_format_repository')
      ->get($value['country_code']);
    $element = static::buildSubdivisionElements($element, $value, $address_format);
    $element = static::buildPostalCodeElements($element, $value, $address_format);
  }
  return $element;
}