function AliasRepository::preloadPathAlias
Same name and namespace in other branches
- 8.9.x core/lib/Drupal/Core/Path/AliasRepository.php \Drupal\Core\Path\AliasRepository::preloadPathAlias()
- 11.x core/modules/path_alias/src/AliasRepository.php \Drupal\path_alias\AliasRepository::preloadPathAlias()
- 10 core/modules/path_alias/src/AliasRepository.php \Drupal\path_alias\AliasRepository::preloadPathAlias()
- 9 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. If match to path in $preload differs by case, the array will contain an entry for the stored path and the provided path. The stored path will be returned first.
Overrides AliasRepositoryInterface::preloadPathAlias
File
-
core/
modules/ path_alias/ src/ AliasRepository.php, line 35
Class
- AliasRepository
- Provides the default path alias lookup operations.
Namespace
Drupal\path_aliasCode
public function preloadPathAlias($preloaded, $langcode) {
$select = $this->getBaseQuery()
->fields('base_table', [
'path',
'alias',
]);
// Use a map of paths to their lowercase equivalents to look up aliases so
// the returned array will contain an exact match to the provided path if it
// differs by case to the stored path.
$path_map = [];
if (!empty($preloaded)) {
$conditions = $this->connection
->condition('OR');
foreach ($preloaded as $preloaded_item) {
$path_map[$preloaded_item] = mb_strtolower($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(FetchAs::Associative);
$aliases = [];
foreach (array_reverse($results) as $result) {
$aliases[$result['path']] = $result['alias'];
// Paths are user input and are looked up via case-insensitive LIKE. If
// the alias's path does not have an exact match, then look up the path
// provided by the user in the map.
if (!isset($path_map[$result['path']])) {
$other_path = array_search(mb_strtolower($result['path']), $path_map);
if ($other_path !== FALSE) {
$aliases[$other_path] = $result['alias'];
}
}
}
return $aliases;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.