You are here

class LinkTypeConstraint in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php \Drupal\link\Plugin\Validation\Constraint\LinkTypeConstraint

Validation constraint for links receiving data allowed by its settings.

Plugin annotation


@Constraint(
  id = "LinkType",
  label = @Translation("Link data valid for link type.", context = "Validation"),
)

Hierarchy

Expanded class hierarchy of LinkTypeConstraint

File

core/modules/link/src/Plugin/Validation/Constraint/LinkTypeConstraint.php, line 23
Contains \Drupal\link\Plugin\Validation\Constraint\LinkTypeConstraint.

Namespace

Drupal\link\Plugin\Validation\Constraint
View source
class LinkTypeConstraint extends Constraint implements ConstraintValidatorInterface {
  public $message = "The path '@uri' is invalid.";

  /**
   * @var \Symfony\Component\Validator\ExecutionContextInterface
   */
  protected $context;

  /**
   * {@inheritDoc}
   */
  public function initialize(ExecutionContextInterface $context) {
    $this->context = $context;
  }

  /**
   * {@inheritdoc}
   */
  public function validatedBy() {
    return get_class($this);
  }

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (isset($value)) {
      $uri_is_valid = TRUE;

      /** @var $link_item \Drupal\link\LinkItemInterface */
      $link_item = $value;
      $link_type = $link_item
        ->getFieldDefinition()
        ->getSetting('link_type');

      // Try to resolve the given URI to a URL. It may fail if it's schemeless.
      try {
        $url = $link_item
          ->getUrl();
      } catch (\InvalidArgumentException $e) {
        $uri_is_valid = FALSE;
      }

      // If the link field doesn't support both internal and external links,
      // check whether the URL (a resolved URI) is in fact violating either
      // restriction.
      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($this->message, array(
          '@uri' => $link_item->uri,
        ));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Constraint::$errorNames protected static property Maps error codes to the names of their constants. 16
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 1
Constraint::CLASS_CONSTRAINT constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 18
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getRequiredOptions public function Returns the name of the required options. 11
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or both. 11
Constraint::PROPERTY_CONSTRAINT constant Marks a constraint that can be put onto properties.
Constraint::__construct public function Initializes the constraint with options. 11
Constraint::__get public function Returns the value of a lazily initialized option. 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
LinkTypeConstraint::$context protected property
LinkTypeConstraint::$message public property
LinkTypeConstraint::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
LinkTypeConstraint::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
LinkTypeConstraint::validatedBy public function Returns the name of the class that validates this constraint. Overrides Constraint::validatedBy