function UniquePathAliasConstraintValidator::validate
Same name in other branches
- 9 core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/UniquePathAliasConstraintValidator.php \Drupal\Core\Path\Plugin\Validation\Constraint\UniquePathAliasConstraintValidator::validate()
- 8.9.x core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/UniquePathAliasConstraintValidator.php \Drupal\Core\Path\Plugin\Validation\Constraint\UniquePathAliasConstraintValidator::validate()
- 10 core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/UniquePathAliasConstraintValidator.php \Drupal\Core\Path\Plugin\Validation\Constraint\UniquePathAliasConstraintValidator::validate()
File
-
core/
lib/ Drupal/ Core/ Path/ Plugin/ Validation/ Constraint/ UniquePathAliasConstraintValidator.php, line 45
Class
- UniquePathAliasConstraintValidator
- Constraint validator for a unique path alias.
Namespace
Drupal\Core\Path\Plugin\Validation\ConstraintCode
public function validate($entity, Constraint $constraint) : void {
/** @var \Drupal\path_alias\PathAliasInterface $entity */
$path = $entity->getPath();
$alias = $entity->getAlias();
$langcode = $entity->language()
->getId();
$storage = $this->entityTypeManager
->getStorage('path_alias');
$query = $storage->getQuery()
->accessCheck(FALSE)
->condition('alias', $alias, '=')
->condition('langcode', $langcode, '=');
if (!$entity->isNew()) {
$query->condition('id', $entity->id(), '<>');
}
if ($path) {
$query->condition('path', $path, '<>');
}
if ($result = $query->range(0, 1)
->execute()) {
$existing_alias_id = reset($result);
$existing_alias = $storage->load($existing_alias_id);
if ($existing_alias->getAlias() !== $alias) {
$this->context
->buildViolation($constraint->differentCapitalizationMessage, [
'%alias' => $alias,
'%stored_alias' => $existing_alias->getAlias(),
])
->addViolation();
}
else {
$this->context
->buildViolation($constraint->message, [
'%alias' => $alias,
])
->addViolation();
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.