View source
<?php
namespace Drupal\Core\Path;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
@trigger_error('\\Drupal\\Core\\Path\\AliasStorage is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the "path_alias.repository" service instead, or the entity storage handler for the "path_alias" entity type for CRUD methods. See https://www.drupal.org/node/3013865.', E_USER_DEPRECATED);
class AliasStorage implements AliasStorageInterface {
const TABLE = 'path_alias';
protected $connection;
protected $moduleHandler;
protected $entityTypeManager;
public function __construct(Connection $connection, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager = NULL) {
$this->connection = $connection;
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager ?: \Drupal::entityTypeManager();
}
public function save($source, $alias, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
if ($source[0] !== '/') {
throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $source));
}
if ($alias[0] !== '/') {
throw new \InvalidArgumentException(sprintf('Alias path %s has to start with a slash.', $alias));
}
if ($pid) {
$path_alias = $this
->getPathAliasEntityStorage()
->load($pid);
$original_values = [
'source' => $path_alias
->getPath(),
'alias' => $path_alias
->getAlias(),
'langcode' => $path_alias
->get('langcode')->value,
];
$path_alias
->setPath($source);
$path_alias
->setAlias($alias);
$path_alias
->set('langcode', $langcode);
}
else {
$path_alias = $this
->getPathAliasEntityStorage()
->create([
'path' => $source,
'alias' => $alias,
'langcode' => $langcode,
]);
}
$path_alias
->save();
$path_alias_values = [
'pid' => $path_alias
->id(),
'source' => $path_alias
->getPath(),
'alias' => $path_alias
->getAlias(),
'langcode' => $path_alias
->get('langcode')->value,
];
if (isset($original_values)) {
$path_alias_values['original'] = $original_values;
}
return $path_alias_values;
}
public function load($conditions) {
$query = $this
->getPathAliasEntityStorage()
->getQuery();
$query
->accessCheck(FALSE);
foreach ($conditions as $field => $value) {
if ($field === 'source') {
$field = 'path';
}
elseif ($field === 'pid') {
$field = 'id';
}
$query
->condition($field, $value, '=');
}
$result = $query
->sort('id', 'DESC')
->range(0, 1)
->execute();
$entities = $this
->getPathAliasEntityStorage()
->loadMultiple($result);
$path_alias = reset($entities);
if ($path_alias) {
return [
'pid' => $path_alias
->id(),
'source' => $path_alias
->getPath(),
'alias' => $path_alias
->getAlias(),
'langcode' => $path_alias
->get('langcode')->value,
];
}
return FALSE;
}
public function delete($conditions) {
$storage = $this
->getPathAliasEntityStorage();
$query = $storage
->getQuery();
$query
->accessCheck(FALSE);
foreach ($conditions as $field => $value) {
if ($field === 'source') {
$field = 'path';
}
elseif ($field === 'pid') {
$field = 'id';
}
$query
->condition($field, $value, '=');
}
$result = $query
->execute();
$storage
->delete($storage
->loadMultiple($result));
}
protected function getBaseQuery() {
$query = $this->connection
->select(static::TABLE, 'base_table');
$query
->condition('base_table.status', 1);
return $query;
}
public function preloadPathAlias($preloaded, $langcode) {
$select = $this
->getBaseQuery()
->fields('base_table', [
'path',
'alias',
]);
if (!empty($preloaded)) {
$conditions = new 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', 'ASC');
return $select
->execute()
->fetchAllKeyed();
}
public function lookupPathAlias($path, $langcode) {
$select = $this
->getBaseQuery()
->fields('base_table', [
'alias',
])
->condition('base_table.path', $this->connection
->escapeLike($path), 'LIKE');
$this
->addLanguageFallback($select, $langcode);
$select
->orderBy('base_table.id', 'DESC');
return $select
->execute()
->fetchField();
}
public function lookupPathSource($alias, $langcode) {
$select = $this
->getBaseQuery()
->fields('base_table', [
'path',
])
->condition('base_table.alias', $this->connection
->escapeLike($alias), 'LIKE');
$this
->addLanguageFallback($select, $langcode);
$select
->orderBy('base_table.id', 'DESC');
return $select
->execute()
->fetchField();
}
protected function addLanguageFallback(SelectInterface $query, $langcode) {
$langcode_list = [
$langcode,
LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
if ($langcode === LanguageInterface::LANGCODE_NOT_SPECIFIED) {
array_pop($langcode_list);
}
elseif ($langcode > LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$query
->orderBy('base_table.langcode', 'DESC');
}
else {
$query
->orderBy('base_table.langcode', 'ASC');
}
$query
->condition('base_table.langcode', $langcode_list, 'IN');
}
public function aliasExists($alias, $langcode, $source = NULL) {
$query = $this->connection
->select(static::TABLE)
->condition('alias', $this->connection
->escapeLike($alias), 'LIKE')
->condition('langcode', $langcode);
if (!empty($source)) {
$query
->condition('path', $this->connection
->escapeLike($source), 'NOT LIKE');
}
$query
->addExpression('1');
$query
->range(0, 1);
return (bool) $query
->execute()
->fetchField();
}
public function languageAliasExists() {
return (bool) $this->connection
->queryRange('SELECT 1 FROM {' . static::TABLE . '} WHERE langcode <> :langcode', 0, 1, [
':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
])
->fetchField();
}
public function getAliasesForAdminListing($header, $keys = NULL) {
$query = $this->connection
->select(static::TABLE)
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
if ($keys) {
$query
->condition('alias', '%' . preg_replace('!\\*+!', '%', $keys) . '%', 'LIKE');
}
$query
->addField(static::TABLE, 'id', 'pid');
$query
->addField(static::TABLE, 'path', 'source');
return $query
->fields(static::TABLE, [
'alias',
'langcode',
])
->orderByHeader($header)
->limit(50)
->execute()
->fetchAll();
}
public function pathHasMatchingAlias($initial_substring) {
if (!$this->moduleHandler
->moduleExists('path_alias')) {
return FALSE;
}
$query = $this
->getBaseQuery();
$query
->addExpression(1);
return (bool) $query
->condition('base_table.path', $this->connection
->escapeLike($initial_substring) . '%', 'LIKE')
->range(0, 1)
->execute()
->fetchField();
}
protected function getPathAliasEntityStorage() {
return $this->entityTypeManager
->getStorage('path_alias');
}
}