You are here

public function ContentLanguageFallbackLimitedFilter::query in Language Hierarchy 8

Same name and namespace in other branches
  1. 2.x src/Plugin/views/filter/ContentLanguageFallbackLimitedFilter.php \Drupal\language_hierarchy\Plugin\views\filter\ContentLanguageFallbackLimitedFilter::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides FilterPluginBase::query

File

src/Plugin/views/filter/ContentLanguageFallbackLimitedFilter.php, line 73

Class

ContentLanguageFallbackLimitedFilter
Filters to the most relevant translation for the current content language.

Namespace

Drupal\language_hierarchy\Plugin\views\filter

Code

public function query() {
  if ($this->value) {
    $this
      ->ensureMyTable();
    $langcode = $this->languageManager
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
      ->getId();
    $this->value = array_values($this->languageManager
      ->getFallbackCandidates([
      'langcode' => $langcode,
      'operation' => 'query',
    ]));

    // Ensure the current language is included in the fallback candidates, as
    // it doesn't necessarily have to be.
    if (!in_array($langcode, $this->value, TRUE)) {
      $this->value = array_merge([
        $langcode,
      ], $this->value);
    }

    /** @var \Drupal\views\Plugin\views\query\Sql $query */
    $query = $this->query;
    $qualified_field = "{$this->tableAlias}.{$this->realField}";
    if (count($this->value) === 1) {
      $query
        ->addWhere($this->options['group'], $qualified_field, $this->value[0], '=');
    }
    else {
      $query
        ->addWhere($this->options['group'], $qualified_field, $this->value, 'IN');

      // Tag so that the join can be applied at query time.
      // @see language_hierarchy_query_language_hierarchy_limit_alter()
      $query
        ->addTag('language_hierarchy_limit');

      // Metadata cannot be stored in views query plugins, so stuff the
      // necessary metadata into the view object.
      // @see language_hierarchy_query_language_hierarchy_limit_alter()
      $data = $this->viewsData
        ->getAll();
      $this->view->build_info['language_hierarchy_limit'][$qualified_field] = [
        'base_table' => $this->table,
        'base_field' => $data[$this->table]['table']['base']['field'],
        'lang_codes' => $this->value,
      ];
    }
  }
}