You are here

public function AliasUniquifier::uniquify in Pathauto 8

Check to ensure a path alias is unique and add suffix variants if necessary.

Given an alias 'content/test' if a path alias with the exact alias already exists, the function will change the alias to 'content/test-0' and will increase the number suffix until it finds a unique alias.

Parameters

string $alias: A string with the alias. Can be altered by reference.

string $source: A string with the path source.

string $langcode: A string with a language code.

Overrides AliasUniquifierInterface::uniquify

File

src/AliasUniquifier.php, line 77

Class

AliasUniquifier
Provides a utility for creating a unique path alias.

Namespace

Drupal\pathauto

Code

public function uniquify(&$alias, $source, $langcode) {
  $config = $this->configFactory
    ->get('pathauto.settings');
  if (!$this
    ->isReserved($alias, $source, $langcode)) {
    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));
}