8.2.x AliasManager.php public AliasManager::getAliasByPath($path, $langcode = NULL)
8.0.x AliasManager.php public AliasManager::getAliasByPath($path, $langcode = NULL)
8.1.x AliasManager.php public AliasManager::getAliasByPath($path, $langcode = NULL)
8.3.x AliasManager.php public AliasManager::getAliasByPath($path, $langcode = NULL)

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.

Throws

\InvalidArgumentException Thrown when the path does not start with a slash.

Overrides AliasManagerInterface::getAliasByPath

File

core/lib/Drupal/Core/Path/AliasManager.php, line 184

Class

AliasManager
The default alias manager implementation.

Namespace

Drupal\Core\Path

Code

public function getAliasByPath($path, $langcode = NULL) {
  if ($path[0] !== '/') {
    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 whitelist, 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->whitelist->get(strtok(trim($path, '/'), '/'))) {
    return $path;
  }

  // During the first call to this method per language, load the expected
  // paths for the page from cache.
  if (empty($this->langcodePreloaded[$langcode])) {
    $this->langcodePreloaded[$langcode] = TRUE;
    $this->lookupMap[$langcode] = array();

    // Load the cached paths that should be used for preloading. This only
    // happens if a cache key has been set.
    if ($this->preloadedPathLookups === FALSE) {
      $this->preloadedPathLookups = array();
      if ($this->cacheKey) {
        if ($cached = $this->cache->get($this->cacheKey)) {
          $this->preloadedPathLookups = $cached->data;
        }
        else {
          $this->cacheNeedsWriting = TRUE;
        }
      }
    }

    // Load paths from cache.
    if (!empty($this->preloadedPathLookups[$langcode])) {
      $this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups[$langcode], $langcode);
      // Keep a record of paths with no alias to avoid querying twice.
      $this->noAlias[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups[$langcode], array_keys($this->lookupMap[$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];
  }

  // Try to load alias from storage.
  if ($alias = $this->storage->lookupPathAlias($path, $langcode)) {
    $this->lookupMap[$langcode][$path] = $alias;
    return $alias;
  }

  // We can't record anything into $this->lookupMap because we didn't find any
  // aliases for this path. Thus cache to $this->noAlias.
  $this->noAlias[$langcode][$path] = TRUE;
  return $path;
}