LinkTypeConstraintValidator.php in Drupal 10
File
core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraintValidator.php
View source
<?php
namespace Drupal\link\Plugin\Validation\Constraint;
use Drupal\link\LinkItemInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class LinkTypeConstraintValidator extends ConstraintValidator {
public function validate($value, Constraint $constraint) {
if (isset($value)) {
$uri_is_valid = TRUE;
$link_item = $value;
$link_type = $link_item
->getFieldDefinition()
->getSetting('link_type');
try {
$url = $link_item
->getUrl();
} catch (\InvalidArgumentException $e) {
$uri_is_valid = FALSE;
}
if ($uri_is_valid && $link_type !== LinkItemInterface::LINK_GENERIC) {
if (!($link_type & LinkItemInterface::LINK_EXTERNAL) && $url
->isExternal()) {
$uri_is_valid = FALSE;
}
if (!($link_type & LinkItemInterface::LINK_INTERNAL) && !$url
->isExternal()) {
$uri_is_valid = FALSE;
}
}
if (!$uri_is_valid) {
$this->context
->addViolation($constraint->message, [
'@uri' => $link_item->uri,
]);
}
}
}
}