function AliasRepository::preloadPathAlias

Same name and namespace in other branches
  1. 9 core/modules/path_alias/src/AliasRepository.php \Drupal\path_alias\AliasRepository::preloadPathAlias()
  2. 8.9.x core/lib/Drupal/Core/Path/AliasRepository.php \Drupal\Core\Path\AliasRepository::preloadPathAlias()
  3. 11.x core/modules/path_alias/src/AliasRepository.php \Drupal\path_alias\AliasRepository::preloadPathAlias()

Pre-loads path alias information for a given list of system paths.

Parameters

array $preloaded: System paths that need preloading of aliases.

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

Return value

string[] System paths (keys) to alias (values) mapping.

Overrides AliasRepositoryInterface::preloadPathAlias

File

core/modules/path_alias/src/AliasRepository.php, line 34

Class

AliasRepository
Provides the default path alias lookup operations.

Namespace

Drupal\path_alias

Code

public function preloadPathAlias($preloaded, $langcode) {
  $select = $this->getBaseQuery()
    ->fields('base_table', [
    'path',
    'alias',
  ]);
  if (!empty($preloaded)) {
    $conditions = $this->connection
      ->condition('OR');
    foreach ($preloaded as $preloaded_item) {
      $conditions->condition('base_table.path', $this->connection
        ->escapeLike($preloaded_item), 'LIKE');
    }
    $select->condition($conditions);
  }
  $this->addLanguageFallback($select, $langcode);
  $select->orderBy('base_table.id', 'DESC');
  // We want the most recently created alias for each source, however that
  // will be at the start of the result-set, so fetch everything and reverse
  // it. Note that it would not be sufficient to reverse the ordering of the
  // 'base_table.id' column, as that would not guarantee other conditions
  // added to the query, such as those in ::addLanguageFallback, would be
  // reversed.
  $results = $select->execute()
    ->fetchAll(\PDO::FETCH_ASSOC);
  $aliases = [];
  foreach (array_reverse($results) as $result) {
    $aliases[$result['path']] = $result['alias'];
  }
  return $aliases;
}

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