function AliasManager::getPathByAlias

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

Overrides AliasManagerInterface::getPathByAlias

File

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

Class

AliasManager
The default alias manager implementation.

Namespace

Drupal\Core\Path

Code

public function getPathByAlias($alias, $langcode = NULL) {
    // 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();
    // If we already know that there are no paths for this alias simply return.
    if (empty($alias) || !empty($this->noPath[$langcode][$alias])) {
        return $alias;
    }
    // Look for the alias within the cached map.
    if (isset($this->lookupMap[$langcode]) && ($path = array_search($alias, $this->lookupMap[$langcode]))) {
        return $path;
    }
    // Look for path in storage.
    if ($path_alias = $this->pathAliasRepository
        ->lookupByAlias($alias, $langcode)) {
        $this->lookupMap[$langcode][$path_alias['path']] = $alias;
        return $path_alias['path'];
    }
    // We can't record anything into $this->lookupMap because we didn't find any
    // paths for this alias. Thus cache to $this->noPath.
    $this->noPath[$langcode][$alias] = TRUE;
    return $alias;
}

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