You are here

class AliasStorageHelper in Pathauto 8

Provides helper methods for accessing alias storage.

Hierarchy

Expanded class hierarchy of AliasStorageHelper

1 string reference to 'AliasStorageHelper'
pathauto.services.yml in ./pathauto.services.yml
pathauto.services.yml
1 service uses AliasStorageHelper
pathauto.alias_storage_helper in ./pathauto.services.yml
Drupal\pathauto\AliasStorageHelper

File

src/AliasStorageHelper.php, line 17

Namespace

Drupal\pathauto
View source
class AliasStorageHelper implements AliasStorageHelperInterface {
  use StringTranslationTrait;

  /**
   * Alias schema max length.
   *
   * @var int
   */
  protected $aliasSchemaMaxLength = 255;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The alias repository.
   *
   * @var \Drupal\path_alias\AliasRepositoryInterface
   */
  protected $aliasRepository;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The messenger.
   *
   * @var \Drupal\pathauto\MessengerInterface
   */
  protected $messenger;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The config factory.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\path_alias\AliasRepositoryInterface $alias_repository
   *   The alias repository.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manger.
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  public function getAliasSchemaMaxLength() {
    return $this->aliasSchemaMaxLength;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $path, $existing_alias = NULL, $op = NULL) {
    $config = $this->configFactory
      ->get('pathauto.settings');

    // Set up all the variables needed to simplify the code below.
    $source = $path['source'];
    $alias = $path['alias'];
    $langcode = $path['language'];
    if ($existing_alias) {

      /** @var \Drupal\path_alias\PathAliasInterface $existing_alias */
      $existing_alias = $this->entityTypeManager
        ->getStorage('path_alias')
        ->load($existing_alias['pid']);
    }

    // Alert users if they are trying to create an alias that is the same as the
    // internal system path.
    if ($source == $alias) {
      $this->messenger
        ->addMessage($this
        ->t('Ignoring alias %alias because it is the same as the internal path.', [
        '%alias' => $alias,
      ]));
      return NULL;
    }

    // Update the existing alias if there is one and the configuration is set to
    // replace it.
    if ($existing_alias && $config
      ->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_DELETE) {

      // Skip replacing the current alias with an identical alias.
      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 {

      // Otherwise, create a new alias.
      $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(),
    ];
  }

  /**
   * {@inheritdoc}
   */
  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'],
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteBySourcePrefix($source) {
    $pids = $this
      ->loadBySourcePrefix($source);
    if ($pids) {
      $this
        ->deleteMultiple($pids);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {

    /** @var \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping */
    $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();
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function loadBySourcePrefix($source) {
    return $this->entityTypeManager
      ->getStorage('path_alias')
      ->getQuery('OR')
      ->condition('path', $source, '=')
      ->condition('path', rtrim($source, '/') . '/', 'STARTS_WITH')
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function countBySourcePrefix($source) {
    return $this->entityTypeManager
      ->getStorage('path_alias')
      ->getQuery('OR')
      ->condition('path', $source, '=')
      ->condition('path', rtrim($source, '/') . '/', 'STARTS_WITH')
      ->count()
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function countAll() {
    return $this->entityTypeManager
      ->getStorage('path_alias')
      ->getQuery()
      ->count()
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMultiple($pids) {
    $this->entityTypeManager
      ->getStorage('path_alias')
      ->delete($this->entityTypeManager
      ->getStorage('path_alias')
      ->loadMultiple($pids));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasStorageHelper::$aliasRepository protected property The alias repository.
AliasStorageHelper::$aliasSchemaMaxLength protected property Alias schema max length.
AliasStorageHelper::$configFactory protected property Config factory.
AliasStorageHelper::$database protected property The database connection.
AliasStorageHelper::$entityTypeManager protected property The entity type manager service.
AliasStorageHelper::$messenger protected property The messenger.
AliasStorageHelper::countAll public function Returns the total count of the url aliases. Overrides AliasStorageHelperInterface::countAll
AliasStorageHelper::countBySourcePrefix public function Returns the count of url aliases for the source. Overrides AliasStorageHelperInterface::countBySourcePrefix
AliasStorageHelper::deleteAll public function Delete all aliases (truncate the path alias entity tables). Overrides AliasStorageHelperInterface::deleteAll
AliasStorageHelper::deleteBySourcePrefix public function Delete all aliases by source url. Overrides AliasStorageHelperInterface::deleteBySourcePrefix
AliasStorageHelper::deleteEntityPathAll public function Delete an entity URL alias and any of its sub-paths. Overrides AliasStorageHelperInterface::deleteEntityPathAll
AliasStorageHelper::deleteMultiple public function Delete multiple URL aliases. Overrides AliasStorageHelperInterface::deleteMultiple
AliasStorageHelper::getAliasSchemaMaxLength public function Fetch the maximum length of the {path_alias}.alias field from the schema. Overrides AliasStorageHelperInterface::getAliasSchemaMaxLength
AliasStorageHelper::loadBySource public function Fetches an existing URL alias given a path and optional language. Overrides AliasStorageHelperInterface::loadBySource
AliasStorageHelper::loadBySourcePrefix public function Fetches an existing URL alias given a path prefix. Overrides AliasStorageHelperInterface::loadBySourcePrefix
AliasStorageHelper::save public function Private function for Pathauto to create an alias. Overrides AliasStorageHelperInterface::save
AliasStorageHelper::__construct public function The config factory.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.