You are here

public function AdministrativeArea::validateOptionsForm in Address 8

Simple validate handler

Overrides FilterPluginBase::validateOptionsForm

File

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

Class

AdministrativeArea
Filter by administrative area.

Namespace

Drupal\address\Plugin\views\filter

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  if (empty($form_state)) {
    return;
  }
  $is_exposed = !empty($this->options['exposed']);
  $country_source = $form_state
    ->getValue([
    'options',
    'country',
    'country_source',
  ]);
  switch ($country_source) {
    case 'argument':
      $country_argument = $form_state
        ->getValue([
        'options',
        'country',
        'country_argument_id',
      ]);
      if (empty($country_argument)) {
        $error = $this
          ->t("The country contextual filter must be defined for this filter to work using 'contextual filter' for the 'Country source'.");
        $form_state
          ->setError($form['country']['country_source'], $error);
      }
      if (empty($is_exposed)) {
        $error = $this
          ->t('This filter must be exposed to use a contextual filter to specify the country.');
        $form_state
          ->setError($form['country']['country_source'], $error);
      }
      break;
    case 'filter':
      $country_filter = $form_state
        ->getValue([
        'options',
        'country',
        'country_filter_id',
      ]);
      if (empty($country_filter)) {
        $error = $this
          ->t("The country filter must be defined for this filter to work using 'exposed filter' for the 'Country source'.");
        $form_state
          ->setError($form['country']['country_source'], $error);
      }
      if (empty($is_exposed)) {
        $error = $this
          ->t('This filter must be exposed to use a filter to specify the country.');
        $form_state
          ->setError($form['country']['country_source'], $error);
      }
      break;
    case 'static':
      $country_code = $form_state
        ->getValue([
        'options',
        'country',
        'country_static_code',
      ]);
      if (empty($country_code)) {
        $error = $this
          ->t('The predefined country must be set for this filter to work.');
        $form_state
          ->setError($form['country']['country_static_code'], $error);
      }
      break;
    default:
      $error = $this
        ->t('The  source for the country must be defined for this filter to work.');
      $form_state
        ->setError($form['country']['country_source'], $error);
      break;
  }
  parent::validateOptionsForm($form, $form_state);
}