Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

LinkExternalProtocolsConstraintValidator.php

Namespace

Drupal\link\Plugin\Validation\Constraint

File

core/modules/link/src/Plugin/Validation/Constraint/LinkExternalProtocolsConstraintValidator.php

View source
<?php

namespace Drupal\link\Plugin\Validation\Constraint;

use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Validates the LinkExternalProtocols constraint.
 */
class LinkExternalProtocolsConstraintValidator extends ConstraintValidator {
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) : void {
        if (isset($value)) {
            try {
                
                /** @var \Drupal\Core\Url $url */
                $url = $value->getUrl();
            } catch (\InvalidArgumentException) {
                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,
                ]);
            }
        }
    }

}

Classes

Title Deprecated Summary
LinkExternalProtocolsConstraintValidator Validates the LinkExternalProtocols constraint.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal