function AliasManager::getAliasByPath

Same name in other branches
  1. 9 core/modules/path_alias/src/AliasManager.php \Drupal\path_alias\AliasManager::getAliasByPath()
  2. 10 core/modules/path_alias/src/AliasManager.php \Drupal\path_alias\AliasManager::getAliasByPath()
  3. 11.x core/modules/path_alias/src/AliasManager.php \Drupal\path_alias\AliasManager::getAliasByPath()

Overrides AliasManagerInterface::getAliasByPath

File

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

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] = [];
        // 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 = [];
            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->pathAliasRepository
                ->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 ($path_alias = $this->pathAliasRepository
        ->lookupBySystemPath($path, $langcode)) {
        $this->lookupMap[$langcode][$path] = $path_alias['alias'];
        return $path_alias['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;
}

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