function LinkNotExistingInternalConstraintValidator::validate

File

core/modules/link/src/Plugin/Validation/Constraint/LinkNotExistingInternalConstraintValidator.php, line 19

Class

LinkNotExistingInternalConstraintValidator
Validates the LinkNotExistingInternal constraint.

Namespace

Drupal\link\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) : void {
  if (isset($value)) {
    try {
      /** @var \Drupal\Core\Url $url */
      $url = $value->getUrl();
    } catch (\InvalidArgumentException) {
      return;
    }
    if ($url->isRouted()) {
      $allowed = TRUE;
      try {
        $url->toString(TRUE);
      } catch (RouteNotFoundException) {
        $allowed = FALSE;
      } catch (InvalidParameterException) {
        $allowed = FALSE;
      } catch (MissingMandatoryParametersException) {
        $allowed = FALSE;
      }
      if (!$allowed) {
        $this->context
          ->buildViolation($constraint->message, [
          '@uri' => $value->uri,
        ])
          ->atPath('uri')
          ->addViolation();
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.