function LinkWidget::validateUriElement
Same name and namespace in other branches
- 11.x core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::validateUriElement()
- 10 core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::validateUriElement()
- 9 core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::validateUriElement()
- 8.9.x core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::validateUriElement()
Form element validation handler for the 'uri' element.
Disallows saving inaccessible or untrusted URLs.
File
-
core/
modules/ link/ src/ Plugin/ Field/ FieldWidget/ LinkWidget.php, line 143
Class
- LinkWidget
- Plugin implementation of the 'link' widget.
Namespace
Drupal\link\Plugin\Field\FieldWidgetCode
public static function validateUriElement($element, FormStateInterface $form_state, $form) {
$uri = static::getUserEnteredStringAsUri($element['#value']);
$form_state->setValueForElement($element, $uri);
// If getUserEnteredStringAsUri() mapped the entered value to an 'internal:'
// URI , ensure the raw value begins with '/', '?' or '#'.
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
if (parse_url($uri, PHP_URL_SCHEME) === 'internal' && !in_array($element['#value'][0], [
'/',
'?',
'#',
], TRUE) && !str_starts_with($element['#value'], '<front>')) {
// Display an error message according to the link type.
$args = [
'%link_example' => 'https://example.com',
];
switch ($element['#link_type']) {
case LinkItemInterface::LINK_EXTERNAL:
$error_message = new TranslatableMarkup('External links must be a full URL including the protocol, such as %link_example.', $args);
break;
case LinkItemInterface::LINK_INTERNAL:
$error_message = new TranslatableMarkup('Enter a content title to select it, or enter an internal path starting with /, ? or #.');
break;
case LinkItemInterface::LINK_GENERIC:
default:
$error_message = new TranslatableMarkup('Enter a content title to select it, or enter an internal path starting with /, ? or #. External links must be a full URL including the protocol, such as %link_example.', $args);
}
$form_state->setError($element, $error_message);
return;
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.