You are here

public function LanguageNegotiationUrl::processInbound in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::processInbound()

Processes the inbound path.

Implementations may make changes to the request object passed in but should avoid all other side effects. This method can be called to process requests other than the current request.

Parameters

string $path: The path to process, with a leading slash.

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the request to process. Note, if this method is being called via the path_processor_manager service and is not part of routing, the current request object must be cloned before being passed in.

Return value

string The processed path.

Overrides InboundPathProcessorInterface::processInbound

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php, line 101

Class

LanguageNegotiationUrl
Class for identifying language via URL prefix or domain.

Namespace

Drupal\language\Plugin\LanguageNegotiation

Code

public function processInbound($path, Request $request) {
  $config = $this->config
    ->get('language.negotiation')
    ->get('url');
  if ($config['source'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
    $parts = explode('/', trim($path, '/'));
    $prefix = array_shift($parts);

    // Search prefix within added languages.
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      if (isset($config['prefixes'][$language
        ->getId()]) && $config['prefixes'][$language
        ->getId()] == $prefix) {

        // Rebuild $path with the language removed.
        $path = '/' . implode('/', $parts);
        break;
      }
    }
  }
  return $path;
}