class PathProcessorLanguage

Same name in other branches
  1. 9 core/modules/language/src/HttpKernel/PathProcessorLanguage.php \Drupal\language\HttpKernel\PathProcessorLanguage
  2. 8.9.x core/modules/language/src/HttpKernel/PathProcessorLanguage.php \Drupal\language\HttpKernel\PathProcessorLanguage
  3. 11.x core/modules/language/src/HttpKernel/PathProcessorLanguage.php \Drupal\language\HttpKernel\PathProcessorLanguage

Processes the inbound path using path alias lookups.

Hierarchy

  • class \Drupal\language\HttpKernel\PathProcessorLanguage implements \Drupal\Core\PathProcessor\InboundPathProcessorInterface, \Drupal\Core\PathProcessor\OutboundPathProcessorInterface

Expanded class hierarchy of PathProcessorLanguage

2 files declare their use of PathProcessorLanguage
ConfigSubscriber.php in core/modules/language/src/EventSubscriber/ConfigSubscriber.php
PathProcessorTest.php in core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php

File

core/modules/language/src/HttpKernel/PathProcessorLanguage.php, line 19

Namespace

Drupal\language\HttpKernel
View source
class PathProcessorLanguage implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
    
    /**
     * A config factory for retrieving required config settings.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $config;
    
    /**
     * Language manager for retrieving the URL language type.
     *
     * @var \Drupal\language\ConfigurableLanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * The language negotiator.
     *
     * @var \Drupal\language\LanguageNegotiatorInterface
     */
    protected $negotiator;
    
    /**
     * Local cache for language path processors.
     *
     * @var array
     */
    protected $processors;
    
    /**
     * Flag indicating whether the site is multilingual.
     *
     * @var bool
     */
    protected $multilingual;
    
    /**
     * The language configuration event subscriber.
     *
     * @var \Drupal\language\EventSubscriber\ConfigSubscriber
     */
    protected $configSubscriber;
    
    /**
     * Constructs a PathProcessorLanguage object.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config
     *   A config factory object for retrieving configuration settings.
     * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
     *   The configurable language manager.
     * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
     *   The language negotiator.
     * @param \Drupal\Core\Session\AccountInterface $current_user
     *   The current active user.
     * @param \Drupal\language\EventSubscriber\ConfigSubscriber $config_subscriber
     *   The language configuration event subscriber.
     */
    public function __construct(ConfigFactoryInterface $config, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, AccountInterface $current_user, ConfigSubscriber $config_subscriber) {
        $this->config = $config;
        $this->languageManager = $language_manager;
        $this->negotiator = $negotiator;
        $this->negotiator
            ->setCurrentUser($current_user);
        $this->configSubscriber = $config_subscriber;
    }
    
    /**
     * {@inheritdoc}
     */
    public function processInbound($path, Request $request) {
        if (!empty($path)) {
            $scope = 'inbound';
            if (!isset($this->processors[$scope])) {
                $this->initProcessors($scope);
            }
            foreach ($this->processors[$scope] as $instance) {
                $path = $instance->processInbound($path, $request);
            }
        }
        return $path;
    }
    
    /**
     * {@inheritdoc}
     */
    public function processOutbound($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL) {
        if (!isset($this->multilingual)) {
            $this->multilingual = $this->languageManager
                ->isMultilingual();
        }
        if ($this->multilingual) {
            $this->negotiator
                ->reset();
            $scope = 'outbound';
            if (!isset($this->processors[$scope])) {
                $this->initProcessors($scope);
            }
            foreach ($this->processors[$scope] as $instance) {
                $path = $instance->processOutbound($path, $options, $request, $bubbleable_metadata);
            }
            // No language dependent path allowed in this mode.
            if (empty($this->processors[$scope])) {
                unset($options['language']);
            }
        }
        return $path;
    }
    
    /**
     * Initializes the local cache for language path processors.
     *
     * @param string $scope
     *   The scope of the processors: "inbound" or "outbound".
     */
    protected function initProcessors($scope) {
        $interface = 'Drupal\\Core\\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface';
        $this->processors[$scope] = [];
        $weights = [];
        foreach ($this->languageManager
            ->getLanguageTypes() as $type) {
            foreach ($this->negotiator
                ->getNegotiationMethods($type) as $method_id => $method) {
                if (!isset($this->processors[$scope][$method_id])) {
                    if (is_subclass_of($method['class'], $interface)) {
                        $this->processors[$scope][$method_id] = $this->negotiator
                            ->getNegotiationMethodInstance($method_id);
                        $weights[$method_id] = $method['weight'];
                    }
                }
            }
        }
        // Sort the processors list, so that their functions are called in the
        // order specified by the weight of the methods.
        uksort($this->processors[$scope], function ($method_id_a, $method_id_b) use ($weights) {
            return $weights[$method_id_a] <=> $weights[$method_id_b];
        });
    }
    
    /**
     * Initializes the injected event subscriber with the language path processor.
     *
     * The language path processor service is registered only on multilingual
     * site configuration, thus we inject it in the event subscriber only when
     * it is initialized.
     */
    public function initConfigSubscriber() {
        $this->configSubscriber
            ->setPathProcessorLanguage($this);
    }
    
    /**
     * Resets the collected processors instances.
     */
    public function reset() {
        $this->processors = [];
    }

}

Members

Title Sort descending Modifiers Object type Summary
PathProcessorLanguage::$config protected property A config factory for retrieving required config settings.
PathProcessorLanguage::$configSubscriber protected property The language configuration event subscriber.
PathProcessorLanguage::$languageManager protected property Language manager for retrieving the URL language type.
PathProcessorLanguage::$multilingual protected property Flag indicating whether the site is multilingual.
PathProcessorLanguage::$negotiator protected property The language negotiator.
PathProcessorLanguage::$processors protected property Local cache for language path processors.
PathProcessorLanguage::initConfigSubscriber public function Initializes the injected event subscriber with the language path processor.
PathProcessorLanguage::initProcessors protected function Initializes the local cache for language path processors.
PathProcessorLanguage::processInbound public function
PathProcessorLanguage::processOutbound public function
PathProcessorLanguage::reset public function Resets the collected processors instances.
PathProcessorLanguage::__construct public function Constructs a PathProcessorLanguage object.

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