Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,266 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Path\PathValidatorInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'link' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "link",
|
||||
* label = @Translation("Link"),
|
||||
* field_types = {
|
||||
* "link"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The path validator service.
|
||||
*
|
||||
* @var \Drupal\Core\Path\PathValidatorInterface
|
||||
*/
|
||||
protected $pathValidator;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$configuration['field_definition'],
|
||||
$configuration['settings'],
|
||||
$configuration['label'],
|
||||
$configuration['view_mode'],
|
||||
$configuration['third_party_settings'],
|
||||
$container->get('path.validator')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new LinkFormatter.
|
||||
*
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the formatter.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
||||
* The definition of the field to which the formatter is associated.
|
||||
* @param array $settings
|
||||
* The formatter settings.
|
||||
* @param string $label
|
||||
* The formatter label display setting.
|
||||
* @param string $view_mode
|
||||
* The view mode.
|
||||
* @param array $third_party_settings
|
||||
* Third party settings.
|
||||
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
|
||||
* The path validator service.
|
||||
*/
|
||||
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, PathValidatorInterface $path_validator) {
|
||||
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||
$this->pathValidator = $path_validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'trim_length' => '80',
|
||||
'url_only' => '',
|
||||
'url_plain' => '',
|
||||
'rel' => '',
|
||||
'target' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements = parent::settingsForm($form, $form_state);
|
||||
|
||||
$elements['trim_length'] = array(
|
||||
'#type' => 'number',
|
||||
'#title' => t('Trim link text length'),
|
||||
'#field_suffix' => t('characters'),
|
||||
'#default_value' => $this->getSetting('trim_length'),
|
||||
'#min' => 1,
|
||||
'#description' => t('Leave blank to allow unlimited link text lengths.'),
|
||||
);
|
||||
$elements['url_only'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('URL only'),
|
||||
'#default_value' => $this->getSetting('url_only'),
|
||||
'#access' => $this->getPluginId() == 'link',
|
||||
);
|
||||
$elements['url_plain'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Show URL as plain text'),
|
||||
'#default_value' => $this->getSetting('url_plain'),
|
||||
'#access' => $this->getPluginId() == 'link',
|
||||
'#states' => array(
|
||||
'visible' => array(
|
||||
':input[name*="url_only"]' => array('checked' => TRUE),
|
||||
),
|
||||
),
|
||||
);
|
||||
$elements['rel'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Add rel="nofollow" to links'),
|
||||
'#return_value' => 'nofollow',
|
||||
'#default_value' => $this->getSetting('rel'),
|
||||
);
|
||||
$elements['target'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Open link in new window'),
|
||||
'#return_value' => '_blank',
|
||||
'#default_value' => $this->getSetting('target'),
|
||||
);
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (!empty($settings['trim_length'])) {
|
||||
$summary[] = t('Link text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
|
||||
}
|
||||
else {
|
||||
$summary[] = t('Link text not trimmed');
|
||||
}
|
||||
if ($this->getPluginId() == 'link' && !empty($settings['url_only'])) {
|
||||
if (!empty($settings['url_plain'])) {
|
||||
$summary[] = t('Show URL only as plain-text');
|
||||
}
|
||||
else {
|
||||
$summary[] = t('Show URL only');
|
||||
}
|
||||
}
|
||||
if (!empty($settings['rel'])) {
|
||||
$summary[] = t('Add rel="@rel"', array('@rel' => $settings['rel']));
|
||||
}
|
||||
if (!empty($settings['target'])) {
|
||||
$summary[] = t('Open link in new window');
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$element = array();
|
||||
$entity = $items->getEntity();
|
||||
$settings = $this->getSettings();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
// By default use the full URL as the link text.
|
||||
$url = $this->buildUrl($item);
|
||||
$link_title = $url->toString();
|
||||
|
||||
// If the title field value is available, use it for the link text.
|
||||
if (empty($settings['url_only']) && !empty($item->title)) {
|
||||
// Unsanitized token replacement here because the entire link title
|
||||
// gets auto-escaped during link generation.
|
||||
$link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
|
||||
}
|
||||
|
||||
// Trim the link text to the desired length.
|
||||
if (!empty($settings['trim_length'])) {
|
||||
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
|
||||
}
|
||||
|
||||
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
|
||||
$element[$delta] = array(
|
||||
'#markup' => SafeMarkup::checkPlain($link_title),
|
||||
);
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
// Piggyback on the metadata attributes, which will be placed in the
|
||||
// field template wrapper, and set the URL value in a content
|
||||
// attribute.
|
||||
// @todo Does RDF need a URL rather than an internal URI here?
|
||||
// @see \Drupal\rdf\Tests\Field\LinkFieldRdfaTest.
|
||||
$content = str_replace('internal:/', '', $item->uri);
|
||||
$item->_attributes += array('content' => $content);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$element[$delta] = array(
|
||||
'#type' => 'link',
|
||||
'#title' => $link_title,
|
||||
'#options' => $url->getOptions(),
|
||||
);
|
||||
$element[$delta]['#url'] = $url;
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
$element[$delta]['#options'] += array ('attributes' => array());
|
||||
$element[$delta]['#options']['attributes'] += $item->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and should not be rendered in the field template.
|
||||
unset($item->_attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the \Drupal\Core\Url object for a link field item.
|
||||
*
|
||||
* @param \Drupal\link\LinkItemInterface $item
|
||||
* The link field item being rendered.
|
||||
*
|
||||
* @return \Drupal\Core\Url
|
||||
* An Url object.
|
||||
*/
|
||||
protected function buildUrl(LinkItemInterface $item) {
|
||||
$url = $item->getUrl() ?: Url::fromRoute('<none>');
|
||||
|
||||
$settings = $this->getSettings();
|
||||
$options = $item->options;
|
||||
|
||||
// Add optional 'rel' attribute to link options.
|
||||
if (!empty($settings['rel'])) {
|
||||
$options['attributes']['rel'] = $settings['rel'];
|
||||
}
|
||||
// Add optional 'target' attribute to link options.
|
||||
if (!empty($settings['target'])) {
|
||||
$options['attributes']['target'] = $settings['target'];
|
||||
}
|
||||
$url->setOptions($options);
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Field\FieldFormatter\LinkSeparateFormatter.
|
||||
*
|
||||
* @todo
|
||||
* Merge into 'link' formatter once there is a #type like 'item' that
|
||||
* can render a compound label and content outside of a form context.
|
||||
* @see https://www.drupal.org/node/1829202
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'link_separate' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "link_separate",
|
||||
* label = @Translation("Separate link text and URL"),
|
||||
* field_types = {
|
||||
* "link"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class LinkSeparateFormatter extends LinkFormatter {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'trim_length' => 80,
|
||||
'rel' => '',
|
||||
'target' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items) {
|
||||
$element = array();
|
||||
$entity = $items->getEntity();
|
||||
$settings = $this->getSettings();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
// By default use the full URL as the link text.
|
||||
$url = $this->buildUrl($item);
|
||||
$link_title = $url->toString();
|
||||
|
||||
// If the link text field value is available, use it for the text.
|
||||
if (empty($settings['url_only']) && !empty($item->title)) {
|
||||
// Unsanitized token replacement here because the entire link title
|
||||
// gets auto-escaped during link generation.
|
||||
$link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
|
||||
}
|
||||
|
||||
// The link_separate formatter has two titles; the link text (as in the
|
||||
// field values) and the URL itself. If there is no link text value,
|
||||
// $link_title defaults to the URL, so it needs to be unset.
|
||||
// The URL version may need to be trimmed as well.
|
||||
if (empty($item->title)) {
|
||||
$link_title = NULL;
|
||||
}
|
||||
$url_title = $url->toString();
|
||||
if (!empty($settings['trim_length'])) {
|
||||
$link_title = Unicode::truncate($link_title, $settings['trim_length'], FALSE, TRUE);
|
||||
$url_title = Unicode::truncate($url_title, $settings['trim_length'], FALSE, TRUE);
|
||||
}
|
||||
|
||||
$element[$delta] = array(
|
||||
'#theme' => 'link_formatter_link_separate',
|
||||
'#title' => $link_title,
|
||||
'#url_title' => $url_title,
|
||||
'#url' => $url,
|
||||
);
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
// Set our RDFa attributes on the <a> element that is being built.
|
||||
$url->setOption('attributes', $item->_attributes);
|
||||
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and should not be rendered in the field template.
|
||||
unset($item->_attributes);
|
||||
}
|
||||
}
|
||||
return $element;
|
||||
}
|
||||
}
|
||||
|
200
core/modules/link/src/Plugin/Field/FieldType/LinkItem.php
Normal file
200
core/modules/link/src/Plugin/Field/FieldType/LinkItem.php
Normal file
|
@ -0,0 +1,200 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Field\FieldType\LinkItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Component\Utility\Random;
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemBase;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
use Drupal\Core\TypedData\MapDataDefinition;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'link' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "link",
|
||||
* label = @Translation("Link"),
|
||||
* description = @Translation("Stores a URL string, optional varchar link text, and optional blob of attributes to assemble a link."),
|
||||
* default_widget = "link_default",
|
||||
* default_formatter = "link",
|
||||
* constraints = {"LinkType" = {}, "LinkAccess" = {}, "LinkExternalProtocols" = {}, "LinkNotExistingInternal" = {}}
|
||||
* )
|
||||
*/
|
||||
class LinkItem extends FieldItemBase implements LinkItemInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultFieldSettings() {
|
||||
return array(
|
||||
'title' => DRUPAL_OPTIONAL,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC
|
||||
) + parent::defaultFieldSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
|
||||
$properties['uri'] = DataDefinition::create('uri')
|
||||
->setLabel(t('URI'));
|
||||
|
||||
$properties['title'] = DataDefinition::create('string')
|
||||
->setLabel(t('Link text'));
|
||||
|
||||
$properties['options'] = MapDataDefinition::create()
|
||||
->setLabel(t('Options'));
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'uri' => array(
|
||||
'description' => 'The URI of the link.',
|
||||
'type' => 'varchar',
|
||||
'length' => 2048,
|
||||
),
|
||||
'title' => array(
|
||||
'description' => 'The link text.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
),
|
||||
'options' => array(
|
||||
'description' => 'Serialized array of options for the link.',
|
||||
'type' => 'blob',
|
||||
'size' => 'big',
|
||||
'serialize' => TRUE,
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'uri' => array(array('uri', 30)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element = array();
|
||||
|
||||
$element['link_type'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Allowed link type'),
|
||||
'#default_value' => $this->getSetting('link_type'),
|
||||
'#options' => array(
|
||||
static::LINK_INTERNAL => t('Internal links only'),
|
||||
static::LINK_EXTERNAL => t('External links only'),
|
||||
static::LINK_GENERIC => t('Both internal and external links'),
|
||||
),
|
||||
);
|
||||
|
||||
$element['title'] = array(
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Allow link text'),
|
||||
'#default_value' => $this->getSetting('title'),
|
||||
'#options' => array(
|
||||
DRUPAL_DISABLED => t('Disabled'),
|
||||
DRUPAL_OPTIONAL => t('Optional'),
|
||||
DRUPAL_REQUIRED => t('Required'),
|
||||
),
|
||||
);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
|
||||
// Set of possible top-level domains.
|
||||
$tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
|
||||
// Set random length for the domain name.
|
||||
$domain_length = mt_rand(7, 15);
|
||||
$random = new Random();
|
||||
|
||||
switch ($field_definition->getSetting('title')) {
|
||||
case DRUPAL_DISABLED:
|
||||
$values['title'] = '';
|
||||
break;
|
||||
case DRUPAL_REQUIRED:
|
||||
$values['title'] = $random->sentences(4);
|
||||
break;
|
||||
case DRUPAL_OPTIONAL:
|
||||
// In case of optional title, randomize its generation.
|
||||
$values['title'] = mt_rand(0,1) ? $random->sentences(4) : '';
|
||||
break;
|
||||
}
|
||||
$values['uri'] = 'http://www.' . $random->word($domain_length) . '.' . $tlds[mt_rand(0, (sizeof($tlds)-1))];
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isEmpty() {
|
||||
$value = $this->get('uri')->getValue();
|
||||
return $value === NULL || $value === '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isExternal() {
|
||||
return $this->getUrl()->isExternal();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function mainPropertyName() {
|
||||
return 'uri';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUrl() {
|
||||
return Url::fromUri($this->uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setValue($values, $notify = TRUE) {
|
||||
// Treat the values as property value of the main property, if no array is
|
||||
// given.
|
||||
if (isset($values) && !is_array($values)) {
|
||||
$values = [static::mainPropertyName() => $values];
|
||||
}
|
||||
if (isset($values)) {
|
||||
$values += [
|
||||
'options' => [],
|
||||
];
|
||||
}
|
||||
// Unserialize the values.
|
||||
// @todo The storage controller should take care of this, see
|
||||
// SqlContentEntityStorage::loadFieldItems, see
|
||||
// https://www.drupal.org/node/2414835
|
||||
if (is_string($values['options'])) {
|
||||
$values['options'] = unserialize($values['options']);
|
||||
}
|
||||
parent::setValue($values, $notify);
|
||||
}
|
||||
|
||||
}
|
372
core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Normal file
372
core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Normal file
|
@ -0,0 +1,372 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Field\FieldWidget\LinkWidget.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Drupal\Core\Entity\Element\EntityAutocomplete;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'link' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "link_default",
|
||||
* label = @Translation("Link"),
|
||||
* field_types = {
|
||||
* "link"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class LinkWidget extends WidgetBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'placeholder_url' => '',
|
||||
'placeholder_title' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the URI without the 'internal:' or 'entity:' scheme.
|
||||
*
|
||||
* The following two forms of URIs are transformed:
|
||||
* - 'entity:' URIs: to entity autocomplete ("label (entity id)") strings;
|
||||
* - 'internal:' URIs: the scheme is stripped.
|
||||
*
|
||||
* This method is the inverse of ::getUserEnteredStringAsUri().
|
||||
*
|
||||
* @param string $uri
|
||||
* The URI to get the displayable string for.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @see static::getUserEnteredStringAsUri()
|
||||
*/
|
||||
protected static function getUriAsDisplayableString($uri) {
|
||||
$scheme = parse_url($uri, PHP_URL_SCHEME);
|
||||
|
||||
// By default, the displayable string is the URI.
|
||||
$displayable_string = $uri;
|
||||
|
||||
// A different displayable string may be chosen in case of the 'internal:'
|
||||
// or 'entity:' built-in schemes.
|
||||
if ($scheme === 'internal') {
|
||||
$uri_reference = explode(':', $uri, 2)[1];
|
||||
|
||||
// @todo '<front>' is valid input for BC reasons, may be removed by
|
||||
// https://www.drupal.org/node/2421941
|
||||
$path = parse_url($uri, PHP_URL_PATH);
|
||||
if ($path === '/') {
|
||||
$uri_reference = '<front>' . substr($uri_reference, 1);
|
||||
}
|
||||
|
||||
$displayable_string = $uri_reference;
|
||||
}
|
||||
elseif ($scheme === 'entity') {
|
||||
list($entity_type, $entity_id) = explode('/', substr($uri, 7), 2);
|
||||
// Show the 'entity:' URI as the entity autocomplete would.
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
if ($entity_manager->getDefinition($entity_type, FALSE) && $entity = \Drupal::entityManager()->getStorage($entity_type)->load($entity_id)) {
|
||||
$displayable_string = EntityAutocomplete::getEntityLabels(array($entity));
|
||||
}
|
||||
}
|
||||
|
||||
return $displayable_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user-entered string as a URI.
|
||||
*
|
||||
* The following two forms of input are mapped to URIs:
|
||||
* - entity autocomplete ("label (entity id)") strings: to 'entity:' URIs;
|
||||
* - strings without a detectable scheme: to 'internal:' URIs.
|
||||
*
|
||||
* This method is the inverse of ::getUriAsDisplayableString().
|
||||
*
|
||||
* @param string $string
|
||||
* The user-entered string.
|
||||
*
|
||||
* @return string
|
||||
* The URI, if a non-empty $uri was passed.
|
||||
*
|
||||
* @see static::getUriAsDisplayableString()
|
||||
*/
|
||||
protected static function getUserEnteredStringAsUri($string) {
|
||||
// By default, assume the entered string is an URI.
|
||||
$uri = $string;
|
||||
|
||||
// Detect entity autocomplete string, map to 'entity:' URI.
|
||||
$entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
|
||||
if ($entity_id !== NULL) {
|
||||
// @todo Support entity types other than 'node'. Will be fixed in
|
||||
// https://www.drupal.org/node/2423093.
|
||||
$uri = 'entity:node/' . $entity_id;
|
||||
}
|
||||
// Detect a schemeless string, map to 'internal:' URI.
|
||||
elseif (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) {
|
||||
// @todo '<front>' is valid input for BC reasons, may be removed by
|
||||
// https://www.drupal.org/node/2421941
|
||||
// - '<front>' -> '/'
|
||||
// - '<front>#foo' -> '/#foo'
|
||||
if (strpos($string, '<front>') === 0) {
|
||||
$string = '/' . substr($string, strlen('<front>'));
|
||||
}
|
||||
$uri = 'internal:' . $string;
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form element validation handler for the 'uri' element.
|
||||
*
|
||||
* Disallows saving inaccessible or untrusted URLs.
|
||||
*/
|
||||
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 a '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) && substr($element['#value'], 0, 7) !== '<front>') {
|
||||
$form_state->setError($element, t('Manually entered paths should start with /, ? or #.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Form element validation handler for the 'title' element.
|
||||
*
|
||||
* Conditionally requires the link title if a URL value was filled in.
|
||||
*/
|
||||
public static function validateTitleElement(&$element, FormStateInterface $form_state, $form) {
|
||||
if ($element['uri']['#value'] !== '' && $element['title']['#value'] === '') {
|
||||
$element['title']['#required'] = TRUE;
|
||||
$form_state->setError($element['title'], t('!name field is required.', array('!name' => $element['title']['#title'])));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
/** @var \Drupal\link\LinkItemInterface $item */
|
||||
$item = $items[$delta];
|
||||
|
||||
$element['uri'] = array(
|
||||
'#type' => 'url',
|
||||
'#title' => $this->t('URL'),
|
||||
'#placeholder' => $this->getSetting('placeholder_url'),
|
||||
// The current field value could have been entered by a different user.
|
||||
// However, if it is inaccessible to the current user, do not display it
|
||||
// to them.
|
||||
'#default_value' => (!$item->isEmpty() && (\Drupal::currentUser()->hasPermission('link to any page') || $item->getUrl()->access())) ? static::getUriAsDisplayableString($item->uri) : NULL,
|
||||
'#element_validate' => array(array(get_called_class(), 'validateUriElement')),
|
||||
'#maxlength' => 2048,
|
||||
'#required' => $element['#required'],
|
||||
);
|
||||
|
||||
// If the field is configured to support internal links, it cannot use the
|
||||
// 'url' form element and we have to do the validation ourselves.
|
||||
if ($this->supportsInternalLinks()) {
|
||||
$element['uri']['#type'] = 'entity_autocomplete';
|
||||
// @todo The user should be able to select an entity type. Will be fixed
|
||||
// in https://www.drupal.org/node/2423093.
|
||||
$element['uri']['#target_type'] = 'node';
|
||||
// Disable autocompletion when the first character is '/', '#' or '?'.
|
||||
$element['uri']['#attributes']['data-autocomplete-first-character-blacklist'] = '/#?';
|
||||
|
||||
// The link widget is doing its own processing in
|
||||
// static::getUriAsDisplayableString().
|
||||
$element['uri']['#process_default_value'] = FALSE;
|
||||
}
|
||||
|
||||
// If the field is configured to allow only internal links, add a useful
|
||||
// element prefix.
|
||||
if (!$this->supportsExternalLinks()) {
|
||||
$element['uri']['#field_prefix'] = rtrim(\Drupal::url('<front>', array(), array('absolute' => TRUE)), '/');
|
||||
}
|
||||
// If the field is configured to allow both internal and external links,
|
||||
// show a useful description.
|
||||
elseif ($this->supportsExternalLinks() && $this->supportsInternalLinks()) {
|
||||
$element['uri']['#description'] = $this->t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => '/node/add', '%url' => 'http://example.com'));
|
||||
}
|
||||
// If the field is configured to allow only external links, show a useful
|
||||
// description.
|
||||
elseif ($this->supportsExternalLinks() && !$this->supportsInternalLinks()) {
|
||||
$element['uri']['#description'] = $this->t('This must be an external URL such as %url.', array('%url' => 'http://example.com'));
|
||||
}
|
||||
|
||||
$element['title'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Link text'),
|
||||
'#placeholder' => $this->getSetting('placeholder_title'),
|
||||
'#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL,
|
||||
'#maxlength' => 255,
|
||||
'#access' => $this->getFieldSetting('title') != DRUPAL_DISABLED,
|
||||
);
|
||||
// Post-process the title field to make it conditionally required if URL is
|
||||
// non-empty. Omit the validation on the field edit form, since the field
|
||||
// settings cannot be saved otherwise.
|
||||
if (!$this->isDefaultValueWidget($form_state) && $this->getFieldSetting('title') == DRUPAL_REQUIRED) {
|
||||
$element['#element_validate'][] = array(get_called_class(), 'validateTitleElement');
|
||||
}
|
||||
|
||||
// Exposing the attributes array in the widget is left for alternate and more
|
||||
// advanced field widgets.
|
||||
$element['attributes'] = array(
|
||||
'#type' => 'value',
|
||||
'#tree' => TRUE,
|
||||
'#value' => !empty($items[$delta]->options['attributes']) ? $items[$delta]->options['attributes'] : array(),
|
||||
'#attributes' => array('class' => array('link-field-widget-attributes')),
|
||||
);
|
||||
|
||||
// If cardinality is 1, ensure a proper label is output for the field.
|
||||
if ($this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1) {
|
||||
// If the link title is disabled, use the field definition label as the
|
||||
// title of the 'uri' element.
|
||||
if ($this->getFieldSetting('title') == DRUPAL_DISABLED) {
|
||||
$element['uri']['#title'] = $element['#title'];
|
||||
}
|
||||
// Otherwise wrap everything in a details element.
|
||||
else {
|
||||
$element += array(
|
||||
'#type' => 'fieldset',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates enabled support for link to routes.
|
||||
*
|
||||
* @return bool
|
||||
* Returns TRUE if the LinkItem field is configured to support links to
|
||||
* routes, otherwise FALSE.
|
||||
*/
|
||||
protected function supportsInternalLinks() {
|
||||
$link_type = $this->getFieldSetting('link_type');
|
||||
return (bool) ($link_type & LinkItemInterface::LINK_INTERNAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates enabled support for link to external URLs.
|
||||
*
|
||||
* @return bool
|
||||
* Returns TRUE if the LinkItem field is configured to support links to
|
||||
* external URLs, otherwise FALSE.
|
||||
*/
|
||||
protected function supportsExternalLinks() {
|
||||
$link_type = $this->getFieldSetting('link_type');
|
||||
return (bool) ($link_type & LinkItemInterface::LINK_EXTERNAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements = parent::settingsForm($form, $form_state);
|
||||
|
||||
$elements['placeholder_url'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Placeholder for URL'),
|
||||
'#default_value' => $this->getSetting('placeholder_url'),
|
||||
'#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
|
||||
);
|
||||
$elements['placeholder_title'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Placeholder for link text'),
|
||||
'#default_value' => $this->getSetting('placeholder_title'),
|
||||
'#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
|
||||
'#states' => array(
|
||||
'invisible' => array(
|
||||
':input[name="instance[settings][title]"]' => array('value' => DRUPAL_DISABLED),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
|
||||
$placeholder_title = $this->getSetting('placeholder_title');
|
||||
$placeholder_url = $this->getSetting('placeholder_url');
|
||||
if (empty($placeholder_title) && empty($placeholder_url)) {
|
||||
$summary[] = $this->t('No placeholders');
|
||||
}
|
||||
else {
|
||||
if (!empty($placeholder_title)) {
|
||||
$summary[] = $this->t('Title placeholder: @placeholder_title', array('@placeholder_title' => $placeholder_title));
|
||||
}
|
||||
if (!empty($placeholder_url)) {
|
||||
$summary[] = $this->t('URL placeholder: @placeholder_url', array('@placeholder_url' => $placeholder_url));
|
||||
}
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
|
||||
foreach ($values as &$value) {
|
||||
$value['uri'] = static::getUserEnteredStringAsUri($value['uri']);
|
||||
$value += ['options' => []];
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Override the '%uri' message parameter, to ensure that 'internal:' URIs
|
||||
* show a validation error message that doesn't mention that scheme.
|
||||
*/
|
||||
public function flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
|
||||
/** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
|
||||
foreach ($violations as $offset => $violation) {
|
||||
$parameters = $violation->getParameters();
|
||||
if (isset($parameters['@uri'])) {
|
||||
$parameters['@uri'] = static::getUriAsDisplayableString($parameters['@uri']);
|
||||
$violations->set($offset, new ConstraintViolation(
|
||||
$this->t($violation->getMessageTemplate(), $parameters),
|
||||
$violation->getMessageTemplate(),
|
||||
$parameters,
|
||||
$violation->getRoot(),
|
||||
$violation->getPropertyPath(),
|
||||
$violation->getInvalidValue(),
|
||||
$violation->getPlural(),
|
||||
$violation->getCode()
|
||||
));
|
||||
}
|
||||
}
|
||||
parent::flagErrors($items, $violations, $form, $form_state);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraint.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Defines an access validation constraint for links.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "LinkAccess",
|
||||
* label = @Translation("Link URI can be accessed by the user.", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
class LinkAccessConstraint extends Constraint {
|
||||
|
||||
public $message = "The path '@uri' is inaccessible.";
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\Session\AccountProxyInterface;
|
||||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Validates the LinkAccess constraint.
|
||||
*/
|
||||
class LinkAccessConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* Stores the validator's state during validation.
|
||||
*
|
||||
* @var \Symfony\Component\Validator\ExecutionContextInterface
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* Proxy for the current user account.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountProxyInterface
|
||||
*/
|
||||
protected $current_user;
|
||||
|
||||
/**
|
||||
* Constructs an instance of the LinkAccessConstraintValidator class.
|
||||
*
|
||||
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
|
||||
* The current user account.
|
||||
*/
|
||||
public function __construct(AccountProxyInterface $current_user) {
|
||||
$this->current_user = $current_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('current_user')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(ExecutionContextInterface $context) {
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint) {
|
||||
if (isset($value)) {
|
||||
try {
|
||||
$url = $value->getUrl();
|
||||
}
|
||||
// If the URL is malformed this constraint cannot check access.
|
||||
catch (\InvalidArgumentException $e) {
|
||||
return;
|
||||
}
|
||||
// Disallow URLs if the current user doesn't have the 'link to any page'
|
||||
// permission nor can access this URI.
|
||||
$allowed = $this->current_user->hasPermission('link to any page') || $url->access();
|
||||
if (!$allowed) {
|
||||
$this->context->addViolation($constraint->message, array('@uri' => $value->uri));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraint.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Defines a protocol validation constraint for links to external URLs.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "LinkExternalProtocols",
|
||||
* label = @Translation("No dangerous external protocols", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
class LinkExternalProtocolsConstraint extends Constraint {
|
||||
|
||||
public $message = "The path '@uri' is invalid.";
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkExternalProtocolsConstraintValidator.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Component\Utility\UrlHelper;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Validates the LinkExternalProtocols constraint.
|
||||
*/
|
||||
class LinkExternalProtocolsConstraintValidator implements ConstraintValidatorInterface {
|
||||
|
||||
/**
|
||||
* Stores the validator's state during validation.
|
||||
*
|
||||
* @var \Symfony\Component\Validator\ExecutionContextInterface
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(ExecutionContextInterface $context) {
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint) {
|
||||
if (isset($value)) {
|
||||
try {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $value->getUrl();
|
||||
}
|
||||
// If the URL is malformed this constraint cannot check further.
|
||||
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, array('@uri' => $value->uri));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraint.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Defines a protocol validation constraint for links to broken internal URLs.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "LinkNotExistingInternal",
|
||||
* label = @Translation("No broken internal links", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
class LinkNotExistingInternalConstraint extends Constraint {
|
||||
|
||||
public $message = "The path '@uri' is invalid.";
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkNotExistingInternalConstraintValidator.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Validates the LinkNotExistingInternal constraint.
|
||||
*/
|
||||
class LinkNotExistingInternalConstraintValidator implements ConstraintValidatorInterface {
|
||||
|
||||
/**
|
||||
* Stores the validator's state during validation.
|
||||
*
|
||||
* @var \Symfony\Component\Validator\ExecutionContextInterface
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(ExecutionContextInterface $context) {
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint) {
|
||||
if (isset($value)) {
|
||||
try {
|
||||
/** @var \Drupal\Core\Url $url */
|
||||
$url = $value->getUrl();
|
||||
}
|
||||
// If the URL is malformed this constraint cannot check further.
|
||||
catch (\InvalidArgumentException $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($url->isRouted()) {
|
||||
$allowed = TRUE;
|
||||
try {
|
||||
$url->toString();
|
||||
}
|
||||
catch (RouteNotFoundException $e) {
|
||||
$allowed = FALSE;
|
||||
}
|
||||
if (!$allowed) {
|
||||
$this->context->addViolation($constraint->message, array('@uri' => $value->uri));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\link\Plugin\Validation\Constraint\LinkTypeConstraint.
|
||||
*/
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
|
||||
/**
|
||||
* Validation constraint for links receiving data allowed by its settings.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "LinkType",
|
||||
* label = @Translation("Link data valid for link type.", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue