View source
<?php
namespace Drupal\pathauto;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\path_alias\AliasRepositoryInterface;
class AliasStorageHelper implements AliasStorageHelperInterface {
use StringTranslationTrait;
protected $aliasSchemaMaxLength = 255;
protected $configFactory;
protected $aliasRepository;
protected $database;
protected $messenger;
protected $entityTypeManager;
public function __construct(ConfigFactoryInterface $config_factory, AliasRepositoryInterface $alias_repository, Connection $database, MessengerInterface $messenger, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager = NULL) {
$this->configFactory = $config_factory;
$this->aliasRepository = $alias_repository;
$this->database = $database;
$this->messenger = $messenger;
$this->stringTranslation = $string_translation;
$this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type.manager');
}
public function getAliasSchemaMaxLength() {
return $this->aliasSchemaMaxLength;
}
public function save(array $path, $existing_alias = NULL, $op = NULL) {
$config = $this->configFactory
->get('pathauto.settings');
$source = $path['source'];
$alias = $path['alias'];
$langcode = $path['language'];
if ($existing_alias) {
$existing_alias = $this->entityTypeManager
->getStorage('path_alias')
->load($existing_alias['pid']);
}
if ($source == $alias) {
$this->messenger
->addMessage($this
->t('Ignoring alias %alias because it is the same as the internal path.', [
'%alias' => $alias,
]));
return NULL;
}
if ($existing_alias && $config
->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_DELETE) {
if ($existing_alias
->getAlias() == $alias) {
return NULL;
}
$old_alias = $existing_alias
->getAlias();
$existing_alias
->setAlias($alias)
->save();
$this->messenger
->addMessage($this
->t('Created new alias %alias for %source, replacing %old_alias.', [
'%alias' => $alias,
'%source' => $source,
'%old_alias' => $old_alias,
]));
$return = $existing_alias;
}
else {
$path_alias = $this->entityTypeManager
->getStorage('path_alias')
->create([
'path' => $source,
'alias' => $alias,
'langcode' => $langcode,
]);
$path_alias
->save();
$this->messenger
->addMessage($this
->t('Created new alias %alias for %source.', [
'%alias' => $path_alias
->getAlias(),
'%source' => $path_alias
->getPath(),
]));
$return = $path_alias;
}
return [
'source' => $return
->getPath(),
'alias' => $return
->getAlias(),
'pid' => $return
->id(),
'langcode' => $return
->language()
->getId(),
];
}
public function loadBySource($source, $language = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$alias = $this->aliasRepository
->lookupBySystemPath($source, $language);
if ($alias) {
return [
'pid' => $alias['id'],
'alias' => $alias['alias'],
'source' => $alias['path'],
'langcode' => $alias['langcode'],
];
}
}
public function deleteBySourcePrefix($source) {
$pids = $this
->loadBySourcePrefix($source);
if ($pids) {
$this
->deleteMultiple($pids);
}
}
public function deleteAll() {
$table_mapping = $this->entityTypeManager
->getStorage('path_alias')
->getTableMapping();
foreach ($table_mapping
->getTableNames() as $table_name) {
$this->database
->truncate($table_name)
->execute();
}
$this->entityTypeManager
->getStorage('path_alias')
->resetCache();
}
public function deleteEntityPathAll(EntityInterface $entity, $default_uri = NULL) {
$this
->deleteBySourcePrefix('/' . $entity
->toUrl('canonical')
->getInternalPath());
if (isset($default_uri) && $entity
->toUrl('canonical')
->toString() != $default_uri) {
$this
->deleteBySourcePrefix($default_uri);
}
}
public function loadBySourcePrefix($source) {
return $this->entityTypeManager
->getStorage('path_alias')
->getQuery('OR')
->condition('path', $source, '=')
->condition('path', rtrim($source, '/') . '/', 'STARTS_WITH')
->execute();
}
public function countBySourcePrefix($source) {
return $this->entityTypeManager
->getStorage('path_alias')
->getQuery('OR')
->condition('path', $source, '=')
->condition('path', rtrim($source, '/') . '/', 'STARTS_WITH')
->count()
->execute();
}
public function countAll() {
return $this->entityTypeManager
->getStorage('path_alias')
->getQuery()
->count()
->execute();
}
public function deleteMultiple($pids) {
$this->entityTypeManager
->getStorage('path_alias')
->delete($this->entityTypeManager
->getStorage('path_alias')
->loadMultiple($pids));
}
}