LanguageFilter.php

Same filename in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/filter/LanguageFilter.php
  2. 10 core/modules/views/src/Plugin/views/filter/LanguageFilter.php
  3. 11.x core/modules/views/src/Plugin/views/filter/LanguageFilter.php

Namespace

Drupal\views\Plugin\views\filter

File

core/modules/views/src/Plugin/views/filter/LanguageFilter.php

View source
<?php

namespace Drupal\views\Plugin\views\filter;

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides filtering by language.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("language")
 */
class LanguageFilter extends InOperator implements ContainerFactoryPluginInterface {
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * Constructs a new LanguageFilter instance.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     */
    public function __construct($configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->languageManager = $language_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('language_manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getValueOptions() {
        if (!isset($this->valueOptions)) {
            $this->valueTitle = $this->t('Language');
            // Pass the current values so options that are already selected do not get
            // lost when there are changes in the language configuration.
            $this->valueOptions = $this->listLanguages(LanguageInterface::STATE_ALL | LanguageInterface::STATE_SITE_DEFAULT | PluginBase::INCLUDE_NEGOTIATED, array_keys($this->value));
        }
        return $this->valueOptions;
    }
    
    /**
     * {@inheritdoc}
     */
    public function query() {
        // Don't filter by language in case the site is not multilingual, because
        // there is no point in doing so.
        if (!$this->languageManager
            ->isMultilingual()) {
            return;
        }
        parent::query();
    }

}

Classes

Title Deprecated Summary
LanguageFilter Provides filtering by language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.