You are here

class DomainAliasUniquifier in Domain Path 8

Provides a utility for creating a unique domain path alias.

Hierarchy

Expanded class hierarchy of DomainAliasUniquifier

1 string reference to 'DomainAliasUniquifier'
domain_path_pathauto.services.yml in modules/domain_path_pathauto/domain_path_pathauto.services.yml
modules/domain_path_pathauto/domain_path_pathauto.services.yml
1 service uses DomainAliasUniquifier
domain_path_pathauto.alias_uniquifier in modules/domain_path_pathauto/domain_path_pathauto.services.yml
Drupal\domain_path_pathauto\DomainAliasUniquifier

File

modules/domain_path_pathauto/src/DomainAliasUniquifier.php, line 12

Namespace

Drupal\domain_path_pathauto
View source
class DomainAliasUniquifier extends AliasUniquifier {

  /**
   * {@inheritdoc}
   */
  public function uniquify(&$alias, $source, $langcode, $domain_id = '') {
    $config = $this->configFactory
      ->get('pathauto.settings');
    if (!$this
      ->isReserved($alias, $source, $langcode, $domain_id)) {
      return;
    }

    // If the alias already exists, generate a new, hopefully unique, variant.
    $maxlength = min($config
      ->get('max_length'), $this->aliasStorageHelper
      ->getAliasSchemaMaxlength());
    $separator = $config
      ->get('separator');
    $original_alias = $alias;
    $i = 0;
    do {

      // Append an incrementing numeric suffix until we find a unique alias.
      $unique_suffix = $separator . $i;
      $alias = Unicode::truncate($original_alias, $maxlength - mb_strlen($unique_suffix), TRUE) . $unique_suffix;
      $i++;
    } while ($this
      ->isReserved($alias, $source, $langcode));
  }

  /**
   * {@inheritdoc}
   */
  public function isReserved($alias, $source, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $domain_id = '') {

    // Check if this domain alias already exists.
    $query = \Drupal::database()
      ->select('domain_path', 'domain_path')
      ->fields('domain_path', [
      'language',
      'source',
      'alias',
    ])
      ->condition('domain_id', $domain_id)
      ->condition('alias', $alias);
    $result = $query
      ->execute()
      ->fetchAssoc();
    if (isset($result['source'])) {
      $existing_source = $result["source"];
      if ($existing_source != $alias) {

        // If it is an alias for the provided source, it is allowed to keep using
        // it. If not, then it is reserved.
        return $existing_source != $source;
      }
    }

    // Then check if there is a route with the same path.
    if ($this
      ->isRoute($alias)) {
      return TRUE;
    }

    // Finally check if any other modules have reserved the alias.
    $args = [
      $alias,
      $source,
      $langcode,
    ];
    $implementations = $this->moduleHandler
      ->getImplementations('pathauto_is_alias_reserved');
    foreach ($implementations as $module) {
      $result = $this->moduleHandler
        ->invoke($module, 'pathauto_is_alias_reserved', $args);
      if (!empty($result)) {

        // As soon as the first module says that an alias is in fact reserved,
        // then there is no point in checking the rest of the modules.
        return TRUE;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasUniquifier::$aliasManager protected property The alias manager.
AliasUniquifier::$aliasStorageHelper protected property The alias storage helper.
AliasUniquifier::$configFactory protected property Config factory.
AliasUniquifier::$moduleHandler protected property The module handler.
AliasUniquifier::$routeProvider protected property The route provider service.
AliasUniquifier::isRoute public function Verify if the given path is a valid route.
AliasUniquifier::__construct public function Creates a new AliasUniquifier.
DomainAliasUniquifier::isReserved public function Checks if an alias is reserved. Overrides AliasUniquifier::isReserved
DomainAliasUniquifier::uniquify public function Check to ensure a path alias is unique and add suffix variants if necessary. Overrides AliasUniquifier::uniquify