3 namespace Drupal\language\HttpKernel;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
8 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
9 use Drupal\Core\Render\BubbleableMetadata;
10 use Drupal\language\ConfigurableLanguageManagerInterface;
11 use Drupal\language\EventSubscriber\ConfigSubscriber;
12 use Drupal\language\LanguageNegotiatorInterface;
13 use Symfony\Component\HttpFoundation\Request;
14 use Drupal\Core\Session\AccountInterface;
17 * Processes the inbound path using path alias lookups.
19 class PathProcessorLanguage implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
22 * A config factory for retrieving required config settings.
24 * @var \Drupal\Core\Config\ConfigFactoryInterface
29 * Language manager for retrieving the url language type.
31 * @var \Drupal\language\ConfigurableLanguageManagerInterface
33 protected $languageManager;
36 * The language negotiator.
38 * @var \Drupal\language\LanguageNegotiatorInterface
40 protected $negotiator;
43 * Local cache for language path processors.
47 protected $processors;
50 * Flag indicating whether the site is multilingual.
54 protected $multilingual;
57 * The language configuration event subscriber.
59 * @var \Drupal\language\EventSubscriber\ConfigSubscriber
61 protected $configSubscriber;
65 * Constructs a PathProcessorLanguage object.
67 * @param \Drupal\Core\Config\ConfigFactoryInterface $config
68 * A config factory object for retrieving configuration settings.
69 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
70 * The configurable language manager.
71 * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
72 * The language negotiator.
73 * @param \Drupal\Core\Session\AccountInterface $current_user
74 * The current active user.
75 * @param \Drupal\language\EventSubscriber\ConfigSubscriber $config_subscriber
76 * The language configuration event subscriber.
78 public function __construct(ConfigFactoryInterface $config, ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, AccountInterface $current_user, ConfigSubscriber $config_subscriber) {
79 $this->config = $config;
80 $this->languageManager = $language_manager;
81 $this->negotiator = $negotiator;
82 $this->negotiator->setCurrentUser($current_user);
83 $this->configSubscriber = $config_subscriber;
89 public function processInbound($path, Request $request) {
92 if (!isset($this->processors[$scope])) {
93 $this->initProcessors($scope);
95 foreach ($this->processors[$scope] as $instance) {
96 $path = $instance->processInbound($path, $request);
105 public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
106 if (!isset($this->multilingual)) {
107 $this->multilingual = $this->languageManager->isMultilingual();
109 if ($this->multilingual) {
110 $this->negotiator->reset();
112 if (!isset($this->processors[$scope])) {
113 $this->initProcessors($scope);
115 foreach ($this->processors[$scope] as $instance) {
116 $path = $instance->processOutbound($path, $options, $request, $bubbleable_metadata);
118 // No language dependent path allowed in this mode.
119 if (empty($this->processors[$scope])) {
120 unset($options['language']);
127 * Initializes the local cache for language path processors.
129 * @param string $scope
130 * The scope of the processors: "inbound" or "outbound".
132 protected function initProcessors($scope) {
133 $interface = '\Drupal\Core\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface';
134 $this->processors[$scope] = [];
136 foreach ($this->languageManager->getLanguageTypes() as $type) {
137 foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
138 if (!isset($this->processors[$scope][$method_id])) {
139 $reflector = new \ReflectionClass($method['class']);
140 if ($reflector->implementsInterface($interface)) {
141 $this->processors[$scope][$method_id] = $this->negotiator->getNegotiationMethodInstance($method_id);
142 $weights[$method_id] = $method['weight'];
148 // Sort the processors list, so that their functions are called in the
149 // order specified by the weight of the methods.
150 uksort($this->processors[$scope], function ($method_id_a, $method_id_b) use ($weights) {
151 $a_weight = $weights[$method_id_a];
152 $b_weight = $weights[$method_id_b];
154 if ($a_weight == $b_weight) {
158 return ($a_weight < $b_weight) ? -1 : 1;
163 * Initializes the injected event subscriber with the language path processor.
165 * The language path processor service is registered only on multilingual
166 * site configuration, thus we inject it in the event subscriber only when
169 public function initConfigSubscriber() {
170 $this->configSubscriber->setPathProcessorLanguage($this);
174 * Resets the collected processors instances.
176 public function reset() {
177 $this->processors = [];