1 path.inc path_alias_uniquify(&$alias, $source, $langcode)

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.

File

core/modules/path/path.inc, line 341
Miscellaneous functions for Path module.

Code

function path_alias_uniquify(&$alias, $source, $langcode) {
  $config = config('path.settings');
  if (!path_is_alias_reserved($alias, $source, $langcode)) {
    return;
  }

  // If the alias already exists, generate a new, hopefully unique, variant.
  $maxlength = min($config->get('max_length'), _path_get_schema_alias_maxlength());
  $separator = $config->get('separator');
  $original_alias = $alias;

  $i = 1;
  do {
    // Append an incrementing numeric suffix until we find a unique alias.
    $unique_suffix = $separator . $i;
    $alias = truncate_utf8($original_alias, $maxlength - backdrop_strlen($unique_suffix, TRUE)) . $unique_suffix;
    $i++;
  } while (path_is_alias_reserved($alias, $source, $langcode));
}