You are here

public function CountryItem::query in Country 8

Same name in this branch
  1. 8 src/Plugin/views/filter/CountryItem.php \Drupal\country\Plugin\views\filter\CountryItem::query()
  2. 8 src/Plugin/views/sort/CountryItem.php \Drupal\country\Plugin\views\sort\CountryItem::query()

Build query.

Override the query so that no filtering takes place if the user doesn't select any options and to process the value in case the widget is an autocomplete.

Overrides InOperator::query

File

src/Plugin/views/filter/CountryItem.php, line 156

Class

CountryItem
Filter by country ISO2.

Namespace

Drupal\country\Plugin\views\filter

Code

public function query() {
  if (!empty($this->value)) {

    // Autocomplete.
    if ($this->options['type'] == 'textfield') {
      $values = [];
      foreach ((array) $this->value as $raw_value) {
        $raw_value = array_map('trim', explode(',', $raw_value));
        foreach ($raw_value as $raw_sub_value) {
          $iso2 = array_search($raw_sub_value, $this
            ->getValueOptions());

          // If country code wasn't found then this is not a valid country
          // name and has to be kept for the query for making query fails.
          $values[] = $iso2 ?: $raw_sub_value;
        }
      }
      $this->value = $values;
    }
  }
  parent::query();
}