You are here

public static function AdministrativeArea::clearValues in Address 8

Clears the administrative area form values when the country changes.

Implemented as an #after_build callback because #after_build runs before validation, allowing the values to be cleared early enough to prevent the "Illegal choice" error.

File

src/Plugin/views/filter/AdministrativeArea.php, line 617

Class

AdministrativeArea
Filter by administrative area.

Namespace

Drupal\address\Plugin\views\filter

Code

public static function clearValues(array $element, FormStateInterface $form_state) {
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (!$triggering_element) {
    return $element;
  }
  $triggering_element_name = end($triggering_element['#parents']);
  if ($triggering_element_name == 'country_static_code' || $triggering_element_name == 'country_source') {
    foreach ($element['#options'] as $key => $option) {
      $element[$key]['#value'] = 0;
    }
    $element['#value'] = [];
    $input =& $form_state
      ->getUserInput();
    $input['options']['value'] = [];
  }
  return $element;
}