You are here

public function AliasUniquifier::isReserved in Pathauto 8

Checks if an alias is reserved.

Parameters

string $alias: The alias.

string $source: The source.

string $langcode: (optional) The language code.

Return value

bool Returns TRUE if the alias is reserved.

Overrides AliasUniquifierInterface::isReserved

1 call to AliasUniquifier::isReserved()
AliasUniquifier::uniquify in src/AliasUniquifier.php
Check to ensure a path alias is unique and add suffix variants if necessary.

File

src/AliasUniquifier.php, line 101

Class

AliasUniquifier
Provides a utility for creating a unique path alias.

Namespace

Drupal\pathauto

Code

public function isReserved($alias, $source, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED) {

  // Check if this alias already exists.
  if ($existing_source = $this->aliasManager
    ->getPathByAlias($alias, $langcode)) {
    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;
}