class LinkExternalProtocolsConstraintValidator

Same name in other branches
  1. 8.9.x core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator
  2. 10 core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator
  3. 11.x core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator

Validates the LinkExternalProtocols constraint.

Hierarchy

Expanded class hierarchy of LinkExternalProtocolsConstraintValidator

1 file declares its use of LinkExternalProtocolsConstraintValidator
LinkExternalProtocolsConstraintValidatorTest.php in core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidatorTest.php

File

core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php, line 12

Namespace

Drupal\link\Plugin\Validation\Constraint
View source
class LinkExternalProtocolsConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) {
        if (isset($value)) {
            try {
                
                /** @var \Drupal\Core\Url $url */
                $url = $value->getUrl();
            } catch (\InvalidArgumentException $e) {
                return;
            }
            // Disallow external URLs using untrusted protocols.
            if ($url->isExternal() && !in_array(parse_url($url->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
                $this->context
                    ->addViolation($constraint->message, [
                    '@uri' => $value->uri,
                ]);
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
LinkExternalProtocolsConstraintValidator::validate public function

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