LinkNotExistingInternalConstraintValidator.php in Drupal 9
File
core/modules/link/src/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidator.php
View source
<?php
namespace Drupal\link\Plugin\Validation\Constraint;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class LinkNotExistingInternalConstraintValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (isset($value)) {
try {
$url = $value
->getUrl();
} catch (\InvalidArgumentException $e) {
return;
}
if ($url
->isRouted()) {
$allowed = TRUE;
try {
$url
->toString(TRUE);
} catch (RouteNotFoundException $e) {
$allowed = FALSE;
} catch (InvalidParameterException $e) {
$allowed = FALSE;
} catch (MissingMandatoryParametersException $e) {
$allowed = FALSE;
}
if (!$allowed) {
$this->context
->addViolation($constraint->message, [
'@uri' => $value->uri,
]);
}
}
}
}
}