You are here

class LanguageNeutralAliasesStorage in Language neutral aliases 8

Language neutral alias storage.

Hierarchy

Expanded class hierarchy of LanguageNeutralAliasesStorage

1 file declares its use of LanguageNeutralAliasesStorage
LanguageNeutralAliasesStorageTest.php in tests/src/Kernel/LanguageNeutralAliasesStorageTest.php

File

src/LanguageNeutralAliasesStorage.php, line 14

Namespace

Drupal\language_neutral_aliases
View source
class LanguageNeutralAliasesStorage extends AliasStorage {

  /**
   * The source/path field name.
   *
   * @var string
   */
  protected $field;

  /**
   * {@inheritdoc}
   */
  public function __construct(Connection $connection, ModuleHandlerInterface $module_handler) {
    parent::__construct($connection, $module_handler);
    $this->field = 'path';

    // Before 8.8 the field was named source.
    if (version_compare(Drupal::VERSION, '8.8', '<')) {
      $this->field = 'source';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function save($source, $alias, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid = NULL) {

    // Enfoce neutral language.
    $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;

    // If attempting to save a non-neutral alias, save as new.
    if ($pid && ($row = parent::load([
      'pid' => $pid,
    ])) && $row && $row['langcode'] != $langcode) {
      $pid = NULL;
    }
    return parent::save($source, $alias, $langcode, $pid);
  }

  /**
   * {@inheritdoc}
   */
  public function load($conditions) {

    // Callers might have opinions about what language version they want, ignore
    // them.
    $conditions['langcode'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    return parent::load($conditions);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($conditions) {
    $conditions['langcode'] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    return parent::delete($conditions);
  }

  /**
   * {@inheritdoc}
   */
  public function preloadPathAlias($preloaded, $langcode) {
    return parent::preloadPathAlias($preloaded, LanguageInterface::LANGCODE_NOT_SPECIFIED);
  }

  /**
   * {@inheritdoc}
   */
  public function lookupPathAlias($path, $langcode) {
    return parent::lookupPathAlias($path, LanguageInterface::LANGCODE_NOT_SPECIFIED);
  }

  /**
   * {@inheritdoc}
   */
  public function lookupPathSource($path, $langcode) {
    return parent::lookupPathSource($path, LanguageInterface::LANGCODE_NOT_SPECIFIED);
  }

  /**
   * {@inheritdoc}
   */
  public function aliasExists($alias, $langcode, $source = NULL) {
    return parent::aliasExists($alias, LanguageInterface::LANGCODE_NOT_SPECIFIED, $source);
  }

  /**
   * {@inheritdoc}
   */
  public function languageAliasExists() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getAliasesForAdminListing($header, $keys = NULL) {
    $query = $this->connection
      ->select(static::TABLE)
      ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
      ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
    $query
      ->condition('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
    if ($keys) {

      // Replace wildcards with PDO wildcards.
      $query
        ->condition('alias', '%' . preg_replace('!\\*+!', '%', $keys) . '%', 'LIKE');
    }
    try {
      return $query
        ->fields(static::TABLE)
        ->orderByHeader($header)
        ->limit(50)
        ->execute()
        ->fetchAll();
    } catch (\Exception $e) {
      $this
        ->catchException($e);
      return [];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function pathHasMatchingAlias($initial_substring) {
    $query = $this->connection
      ->select(static::TABLE, 'u');
    $query
      ->addExpression(1);
    try {
      return (bool) $query
        ->condition('u.' . $this->field, $this->connection
        ->escapeLike($initial_substring) . '%', 'LIKE')
        ->condition('u.langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)
        ->range(0, 1)
        ->execute()
        ->fetchField();
    } catch (\Exception $e) {
      $this
        ->catchException($e);
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasStorage::$connection protected property The database connection.
AliasStorage::$entityTypeManager protected property The entity type manager.
AliasStorage::$moduleHandler protected property The module handler.
AliasStorage::addLanguageFallback protected function Adds path alias language fallback conditions to a select query object.
AliasStorage::getBaseQuery protected function Returns a SELECT query for the path_alias base table.
AliasStorage::getPathAliasEntityStorage protected function Returns the path alias entity storage handler.
AliasStorage::TABLE constant The table for the url_alias storage.
LanguageNeutralAliasesStorage::$field protected property The source/path field name.
LanguageNeutralAliasesStorage::aliasExists public function Checks if alias already exists. Overrides AliasStorage::aliasExists
LanguageNeutralAliasesStorage::delete public function Deletes a URL alias. Overrides AliasStorage::delete
LanguageNeutralAliasesStorage::getAliasesForAdminListing public function Loads aliases for admin listing. Overrides AliasStorage::getAliasesForAdminListing
LanguageNeutralAliasesStorage::languageAliasExists public function Checks if there are any aliases with language defined. Overrides AliasStorage::languageAliasExists
LanguageNeutralAliasesStorage::load public function Fetches a specific URL alias from the database. Overrides AliasStorage::load
LanguageNeutralAliasesStorage::lookupPathAlias public function Returns an alias of Drupal system URL. Overrides AliasStorage::lookupPathAlias
LanguageNeutralAliasesStorage::lookupPathSource public function Returns Drupal system URL of an alias. Overrides AliasStorage::lookupPathSource
LanguageNeutralAliasesStorage::pathHasMatchingAlias public function Check if any alias exists starting with $initial_substring. Overrides AliasStorage::pathHasMatchingAlias
LanguageNeutralAliasesStorage::preloadPathAlias public function Pre-loads path alias information for a given list of source paths. Overrides AliasStorage::preloadPathAlias
LanguageNeutralAliasesStorage::save public function Saves a path alias to the database. Overrides AliasStorage::save
LanguageNeutralAliasesStorage::__construct public function Constructs a Path CRUD object. Overrides AliasStorage::__construct