Chris@0: config = $config; Chris@0: $this->languageManager = $language_manager; Chris@0: $this->negotiator = $negotiator; Chris@0: $this->negotiator->setCurrentUser($current_user); Chris@0: $this->configSubscriber = $config_subscriber; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function processInbound($path, Request $request) { Chris@0: if (!empty($path)) { Chris@0: $scope = 'inbound'; Chris@0: if (!isset($this->processors[$scope])) { Chris@0: $this->initProcessors($scope); Chris@0: } Chris@0: foreach ($this->processors[$scope] as $instance) { Chris@0: $path = $instance->processInbound($path, $request); Chris@0: } Chris@0: } Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) { Chris@0: if (!isset($this->multilingual)) { Chris@0: $this->multilingual = $this->languageManager->isMultilingual(); Chris@0: } Chris@0: if ($this->multilingual) { Chris@0: $this->negotiator->reset(); Chris@0: $scope = 'outbound'; Chris@0: if (!isset($this->processors[$scope])) { Chris@0: $this->initProcessors($scope); Chris@0: } Chris@0: foreach ($this->processors[$scope] as $instance) { Chris@0: $path = $instance->processOutbound($path, $options, $request, $bubbleable_metadata); Chris@0: } Chris@0: // No language dependent path allowed in this mode. Chris@0: if (empty($this->processors[$scope])) { Chris@0: unset($options['language']); Chris@0: } Chris@0: } Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the local cache for language path processors. Chris@0: * Chris@0: * @param string $scope Chris@0: * The scope of the processors: "inbound" or "outbound". Chris@0: */ Chris@0: protected function initProcessors($scope) { Chris@0: $interface = '\Drupal\Core\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface'; Chris@0: $this->processors[$scope] = []; Chris@0: $weights = []; Chris@0: foreach ($this->languageManager->getLanguageTypes() as $type) { Chris@0: foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) { Chris@0: if (!isset($this->processors[$scope][$method_id])) { Chris@0: $reflector = new \ReflectionClass($method['class']); Chris@0: if ($reflector->implementsInterface($interface)) { Chris@0: $this->processors[$scope][$method_id] = $this->negotiator->getNegotiationMethodInstance($method_id); Chris@0: $weights[$method_id] = $method['weight']; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Sort the processors list, so that their functions are called in the Chris@0: // order specified by the weight of the methods. Chris@0: uksort($this->processors[$scope], function ($method_id_a, $method_id_b) use ($weights) { Chris@0: $a_weight = $weights[$method_id_a]; Chris@0: $b_weight = $weights[$method_id_b]; Chris@0: Chris@0: if ($a_weight == $b_weight) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: return ($a_weight < $b_weight) ? -1 : 1; Chris@0: }); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the injected event subscriber with the language path processor. Chris@0: * Chris@0: * The language path processor service is registered only on multilingual Chris@0: * site configuration, thus we inject it in the event subscriber only when Chris@0: * it is initialized. Chris@0: */ Chris@0: public function initConfigSubscriber() { Chris@0: $this->configSubscriber->setPathProcessorLanguage($this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets the collected processors instances. Chris@0: */ Chris@0: public function reset() { Chris@0: $this->processors = []; Chris@0: } Chris@0: Chris@0: }