You are here

public function AdministrativeArea::buildOptionsForm in Address 8

Provide the basic form which calls through to subforms. If overridden, it is best to call through to the parent, or to at least make sure all of the functions in this form are called.

Overrides FilterPluginBase::buildOptionsForm

File

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

Class

AdministrativeArea
Filter by administrative area.

Namespace

Drupal\address\Plugin\views\filter

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  $this->formState = $form_state;
  $form['country'] = [
    '#type' => 'container',
    '#weight' => -300,
  ];
  $form['country']['country_source'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Country source'),
    '#options' => [
      'static' => $this
        ->t('A predefined country code'),
      'argument' => $this
        ->t('The value of a contextual filter'),
      'filter' => $this
        ->t('The value of an exposed filter'),
    ],
    '#default_value' => $this->options['country']['country_source'],
    '#ajax' => [
      'callback' => [
        get_class($this),
        'ajaxRefreshCountry',
      ],
      'wrapper' => 'admin-area-value-options-ajax-wrapper',
    ],
  ];
  $argument_options = [];

  // Find all the contextual filters on the display to use as options.
  foreach ($this->view->display_handler
    ->getHandlers('argument') as $name => $argument) {

    // @todo Limit this to arguments pointing to a country code field.
    // @see https://www.drupal.org/project/address/issues/3088084
    $argument_options[$name] = $argument
      ->adminLabel();
  }
  if (!empty($argument_options)) {
    $form['country']['country_argument_id'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Country contextual filter'),
      '#options' => $argument_options,
      '#default_value' => $this->options['country']['country_argument_id'],
    ];
  }
  else {

    // #states doesn't work on markup elements, so use a container.
    $form['country']['country_argument_id'] = [
      '#type' => 'container',
    ];
    $form['country']['country_argument_id']['error'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('You must add a contextual filter for the country code to use this filter for administrative areas.'),
    ];
  }
  $form['country']['country_argument_id']['#states'] = [
    'visible' => [
      ':input[name="options[country][country_source]"]' => [
        'value' => 'argument',
      ],
    ],
  ];
  $filter_options = [];

  // Find all country filters from address.module for the valid choices.
  foreach ($this->view->display_handler
    ->getHandlers('filter') as $name => $filter) {
    $definition = $filter->pluginDefinition;

    // Support both 'country' (current) and 'country_code' (deprecated).
    if ($definition['provider'] === 'address' && ($definition['id'] === 'country' || $definition['id'] === 'country_code')) {
      $filter_options[$name] = $filter
        ->adminLabel();
    }
  }
  if (!empty($filter_options)) {
    $form['country']['country_filter_id'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Exposed country filter to determine values'),
      '#options' => $filter_options,
      '#default_value' => $this->options['country']['country_filter_id'],
    ];
  }
  else {

    // #states doesn't work on markup elements, so we to use a container.
    $form['country']['country_filter_id'] = [
      '#type' => 'container',
    ];
    $form['country']['country_filter_id']['error'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('You must add a filter for the country code to use this filter for administrative areas.'),
    ];
  }
  $form['country']['country_filter_id']['#states'] = [
    'visible' => [
      ':input[name="options[country][country_source]"]' => [
        'value' => 'filter',
      ],
    ],
  ];
  $countries = $this
    ->getAdministrativeAreaCountries();
  $form['country']['country_static_code'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Predefined country for administrative areas'),
    '#options' => $countries,
    '#empty_value' => '',
    '#default_value' => $this->options['country']['country_static_code'],
    '#ajax' => [
      'callback' => [
        get_class($this),
        'ajaxRefreshCountry',
      ],
      'wrapper' => 'admin-area-value-options-ajax-wrapper',
    ],
    '#states' => [
      'visible' => [
        ':input[name="options[country][country_source]"]' => [
          'value' => 'static',
        ],
      ],
    ],
  ];

  // @todo This should appear directly above $form['expose']['label'].
  $form['expose']['label_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Label type'),
    '#options' => [
      'static' => $this
        ->t('Static'),
      'dynamic' => $this
        ->t('Dynamic (an appropriate label will be set based on the active country)'),
    ],
    '#default_value' => $this->options['expose']['label_type'],
    '#states' => [
      'visible' => [
        ':input[name="options[expose_button][checkbox][checkbox]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  parent::buildOptionsForm($form, $form_state);
}