LanguageFilter.php

Same filename and directory in other branches
  1. 9 core/modules/views/src/Plugin/views/filter/LanguageFilter.php
  2. 8.9.x core/modules/views/src/Plugin/views/filter/LanguageFilter.php
  3. 10 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\Core\Session\AccountInterface;
use Drupal\views\Attribute\ViewsFilter;
use Drupal\views\Plugin\views\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides filtering by language.
 *
 * @ingroup views_filter_handlers
 */
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() {
        // No point in displaying the language filter on monolingual sites,
        // as only one language value is available.
        if (!$this->languageManager
            ->isMultilingual()) {
            return;
        }
        parent::query();
    }
    
    /**
     * {@inheritdoc}
     */
    public function access(AccountInterface $account) : bool {
        return $this->languageManager
            ->isMultilingual() && parent::access($account);
    }

}

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.