protected function AliasManager::lookupPathAlias

Given a Drupal system URL return one of its aliases if such a one exists. Otherwise, return FALSE.

Parameters

$path: The path to investigate for corresponding aliases.

$langcode: Optional language code to search the path with. Defaults to the page language. If there's no path defined for that language it will search paths without language.

Return value

An aliased path, or FALSE if no path was found.

1 call to AliasManager::lookupPathAlias()

File

drupal/core/lib/Drupal/Core/Path/AliasManager.php, line 169
Contains Drupal\Core\Path\AliasManager.

Class

AliasManager

Namespace

Drupal\Core\Path

Code

protected function lookupPathAlias($path, $langcode) {

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

    // Load system paths from cache.
    if (!empty($this->preloadedPathLookups)) {

      // Now fetch the aliases corresponding to these system paths.
      $args = array(
        ':system' => $this->preloadedPathLookups,
        ':langcode' => $langcode,
        ':langcode_undetermined' => LANGUAGE_NOT_SPECIFIED,
      );

      // Always get the language-specific alias before the language-neutral
      // one. For example 'de' is less than 'und' so the order needs to be
      // ASC, while 'xx-lolspeak' is more than 'und' so the order needs to
      // be DESC. We also order by pid ASC so that fetchAllKeyed() returns
      // the most recently created alias for each source. Subsequent queries
      // using fetchField() must use pid DESC to have the same effect.
      // For performance reasons, the query builder is not used here.
      if ($langcode == LANGUAGE_NOT_SPECIFIED) {

        // Prevent PDO from complaining about a token the query doesn't use.
        unset($args[':langcode']);
        $result = $this->connection
          ->query('SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode = :langcode_undetermined ORDER BY pid ASC', $args);
      }
      elseif ($langcode < LANGUAGE_NOT_SPECIFIED) {
        $result = $this->connection
          ->query('SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode ASC, pid ASC', $args);
      }
      else {
        $result = $this->connection
          ->query('SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode DESC, pid ASC', $args);
      }
      $this->lookupMap[$langcode] = $result
        ->fetchAllKeyed();

      // Keep a record of paths with no alias to avoid querying twice.
      $this->noAliases[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups, array_keys($this->lookupMap[$langcode])));
    }
  }

  // If the alias has already been loaded, return it.
  if (isset($this->lookupMap[$langcode][$path])) {
    return $this->lookupMap[$langcode][$path];
  }
  elseif (!isset($this->whitelist[strtok($path, '/')])) {
    return FALSE;
  }
  elseif (!isset($this->noAliases[$langcode][$path])) {
    $args = array(
      ':source' => $path,
      ':langcode' => $langcode,
      ':langcode_undetermined' => LANGUAGE_NOT_SPECIFIED,
    );

    // See the queries above.
    if ($langcode == LANGUAGE_NOT_SPECIFIED) {
      unset($args[':langcode']);
      $alias = $this->connection
        ->query("SELECT alias FROM {url_alias} WHERE source = :source AND langcode = :langcode_undetermined ORDER BY pid DESC", $args)
        ->fetchField();
    }
    elseif ($langcode > LANGUAGE_NOT_SPECIFIED) {
      $alias = $this->connection
        ->query("SELECT alias FROM {url_alias} WHERE source = :source AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode DESC, pid DESC", $args)
        ->fetchField();
    }
    else {
      $alias = $this->connection
        ->query("SELECT alias FROM {url_alias} WHERE source = :source AND langcode IN (:langcode, :langcode_undetermined) ORDER BY langcode ASC, pid DESC", $args)
        ->fetchField();
    }
    $this->lookupMap[$langcode][$path] = $alias;
    return $alias;
  }
  return FALSE;
}