function AliasManager::getAliasByPath

Given a path, return the alias.

Parameters

string $path: A path.

string $langcode: An optional language code to look up the path in.

Return value

string An alias that represents the path, or path if no alias was found.

Overrides AliasManagerInterface::getAliasByPath

File

core/modules/path_alias/src/AliasManager.php, line 108

Class

AliasManager
The default alias manager implementation.

Namespace

Drupal\path_alias

Code

public function getAliasByPath($path, $langcode = NULL) {
  if (!str_starts_with($path, '/')) {
    throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
  }
  // If no language is explicitly specified we default to the current URL
  // language. If we used a language different from the one conveyed by the
  // requested URL, we might end up being unable to check if there is a path
  // alias matching the URL path.
  $langcode = $langcode ?: $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_URL)
    ->getId();
  // Check the path prefix, if the top-level part before the first / is not in
  // the list, then there is no need to do anything further, it is not in the
  // database.
  if ($path === '/' || !$this->pathPrefixes
    ->get(strtok(trim($path, '/'), '/'))) {
    return $path;
  }
  // If we already know that there are no aliases for this path simply return.
  if (!empty($this->noAlias[$langcode][$path])) {
    return $path;
  }
  // If the alias has already been loaded, return it from static cache.
  if (isset($this->lookupMap[$langcode][$path])) {
    return $this->lookupMap[$langcode][$path];
  }
  // Add the path to the list of requested paths.
  $this->requestedPaths[$langcode][$path] = $path;
  // If we're inside a Fiber, suspend now, this allows other fibers to collect
  // more requested paths.
  if (\Fiber::getCurrent() !== NULL) {
    \Fiber::suspend();
  }
  // If we reach here, then either there are no other Fibers, or none of them
  // have aliases left to look up. Check the static caches in case the path
  // we're looking for was looked up in the meantime.
  if (!empty($this->noAlias[$langcode][$path])) {
    return $path;
  }
  // If the alias has already been loaded, return it from static cache.
  if (isset($this->lookupMap[$langcode][$path])) {
    return $this->lookupMap[$langcode][$path];
  }
  $this->lookupMap[$langcode] = array_merge($this->lookupMap[$langcode] ?? [], $this->pathAliasRepository
    ->preloadPathAlias($this->requestedPaths[$langcode], $langcode));
  // Keep a record of paths with no alias to avoid querying twice.
  $this->noAlias[$langcode] = array_merge($this->noAlias[$langcode] ?? [], array_diff_key($this->requestedPaths[$langcode], $this->lookupMap[$langcode]));
  // Unset the requested paths variable now they've been loaded.
  unset($this->requestedPaths[$langcode]);
  // If we already know that there are no aliases for this path simply return.
  if (!empty($this->noAlias[$langcode][$path])) {
    return $path;
  }
  // If the alias has already been loaded, return it from static cache.
  if (isset($this->lookupMap[$langcode][$path])) {
    return $this->lookupMap[$langcode][$path];
  }
  return $path;
}

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