updated core and modules
This commit is contained in:
@@ -78,13 +78,13 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'trim_length' => '80',
|
||||
'url_only' => '',
|
||||
'url_plain' => '',
|
||||
'rel' => '',
|
||||
'target' => '',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,43 +93,43 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements = parent::settingsForm($form, $form_state);
|
||||
|
||||
$elements['trim_length'] = array(
|
||||
$elements['trim_length'] = [
|
||||
'#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(
|
||||
];
|
||||
$elements['url_only'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('URL only'),
|
||||
'#default_value' => $this->getSetting('url_only'),
|
||||
'#access' => $this->getPluginId() == 'link',
|
||||
);
|
||||
$elements['url_plain'] = array(
|
||||
];
|
||||
$elements['url_plain'] = [
|
||||
'#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(
|
||||
'#states' => [
|
||||
'visible' => [
|
||||
':input[name*="url_only"]' => ['checked' => TRUE],
|
||||
],
|
||||
],
|
||||
];
|
||||
$elements['rel'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Add rel="nofollow" to links'),
|
||||
'#return_value' => 'nofollow',
|
||||
'#default_value' => $this->getSetting('rel'),
|
||||
);
|
||||
$elements['target'] = array(
|
||||
];
|
||||
$elements['target'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Open link in new window'),
|
||||
'#return_value' => '_blank',
|
||||
'#default_value' => $this->getSetting('target'),
|
||||
);
|
||||
];
|
||||
|
||||
return $elements;
|
||||
}
|
||||
@@ -138,12 +138,12 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary = [];
|
||||
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (!empty($settings['trim_length'])) {
|
||||
$summary[] = t('Link text trimmed to @limit characters', array('@limit' => $settings['trim_length']));
|
||||
$summary[] = t('Link text trimmed to @limit characters', ['@limit' => $settings['trim_length']]);
|
||||
}
|
||||
else {
|
||||
$summary[] = t('Link text not trimmed');
|
||||
@@ -157,7 +157,7 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
}
|
||||
}
|
||||
if (!empty($settings['rel'])) {
|
||||
$summary[] = t('Add rel="@rel"', array('@rel' => $settings['rel']));
|
||||
$summary[] = t('Add rel="@rel"', ['@rel' => $settings['rel']]);
|
||||
}
|
||||
if (!empty($settings['target'])) {
|
||||
$summary[] = t('Open link in new window');
|
||||
@@ -170,7 +170,7 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$element = array();
|
||||
$element = [];
|
||||
$entity = $items->getEntity();
|
||||
$settings = $this->getSettings();
|
||||
|
||||
@@ -193,9 +193,9 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
}
|
||||
|
||||
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
|
||||
$element[$delta] = array(
|
||||
$element[$delta] = [
|
||||
'#plain_text' => $link_title,
|
||||
);
|
||||
];
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
// Piggyback on the metadata attributes, which will be placed in the
|
||||
@@ -204,19 +204,19 @@ class LinkFormatter extends FormatterBase implements ContainerFactoryPluginInter
|
||||
// @todo Does RDF need a URL rather than an internal URI here?
|
||||
// @see \Drupal\Tests\rdf\Kernel\Field\LinkFieldRdfaTest.
|
||||
$content = str_replace('internal:/', '', $item->uri);
|
||||
$item->_attributes += array('content' => $content);
|
||||
$item->_attributes += ['content' => $content];
|
||||
}
|
||||
}
|
||||
else {
|
||||
$element[$delta] = array(
|
||||
$element[$delta] = [
|
||||
'#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' => []];
|
||||
$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.
|
||||
|
||||
@@ -26,18 +26,18 @@ class LinkSeparateFormatter extends LinkFormatter {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'trim_length' => 80,
|
||||
'rel' => '',
|
||||
'target' => '',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$element = array();
|
||||
$element = [];
|
||||
$entity = $items->getEntity();
|
||||
$settings = $this->getSettings();
|
||||
|
||||
@@ -67,12 +67,12 @@ class LinkSeparateFormatter extends LinkFormatter {
|
||||
$url_title = Unicode::truncate($url_title, $settings['trim_length'], FALSE, TRUE);
|
||||
}
|
||||
|
||||
$element[$delta] = array(
|
||||
$element[$delta] = [
|
||||
'#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.
|
||||
|
||||
@@ -30,10 +30,10 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultFieldSettings() {
|
||||
return array(
|
||||
return [
|
||||
'title' => DRUPAL_OPTIONAL,
|
||||
'link_type' => LinkItemInterface::LINK_GENERIC
|
||||
) + parent::defaultFieldSettings();
|
||||
] + parent::defaultFieldSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,58 +56,58 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'uri' => array(
|
||||
return [
|
||||
'columns' => [
|
||||
'uri' => [
|
||||
'description' => 'The URI of the link.',
|
||||
'type' => 'varchar',
|
||||
'length' => 2048,
|
||||
),
|
||||
'title' => array(
|
||||
],
|
||||
'title' => [
|
||||
'description' => 'The link text.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
),
|
||||
'options' => array(
|
||||
],
|
||||
'options' => [
|
||||
'description' => 'Serialized array of options for the link.',
|
||||
'type' => 'blob',
|
||||
'size' => 'big',
|
||||
'serialize' => TRUE,
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'uri' => array(array('uri', 30)),
|
||||
),
|
||||
);
|
||||
],
|
||||
],
|
||||
'indexes' => [
|
||||
'uri' => [['uri', 30]],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element = array();
|
||||
$element = [];
|
||||
|
||||
$element['link_type'] = array(
|
||||
$element['link_type'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Allowed link type'),
|
||||
'#default_value' => $this->getSetting('link_type'),
|
||||
'#options' => array(
|
||||
'#options' => [
|
||||
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(
|
||||
$element['title'] = [
|
||||
'#type' => 'radios',
|
||||
'#title' => t('Allow link text'),
|
||||
'#default_value' => $this->getSetting('title'),
|
||||
'#options' => array(
|
||||
'#options' => [
|
||||
DRUPAL_DISABLED => t('Disabled'),
|
||||
DRUPAL_OPTIONAL => t('Optional'),
|
||||
DRUPAL_REQUIRED => t('Required'),
|
||||
),
|
||||
);
|
||||
],
|
||||
];
|
||||
|
||||
return $element;
|
||||
}
|
||||
@@ -119,7 +119,7 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
|
||||
$random = new Random();
|
||||
if ($field_definition->getItemDefinition()->getSetting('link_type') & LinkItemInterface::LINK_EXTERNAL) {
|
||||
// Set of possible top-level domains.
|
||||
$tlds = array('com', 'net', 'gov', 'org', 'edu', 'biz', 'info');
|
||||
$tlds = ['com', 'net', 'gov', 'org', 'edu', 'biz', 'info'];
|
||||
// Set random length for the domain name.
|
||||
$domain_length = mt_rand(7, 15);
|
||||
|
||||
@@ -169,7 +169,7 @@ class LinkItem extends FieldItemBase implements LinkItemInterface {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUrl() {
|
||||
return Url::fromUri($this->uri, $this->options);
|
||||
return Url::fromUri($this->uri, (array) $this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,10 +27,10 @@ class LinkWidget extends WidgetBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
return [
|
||||
'placeholder_url' => '',
|
||||
'placeholder_title' => '',
|
||||
) + parent::defaultSettings();
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,9 +72,10 @@ class LinkWidget extends WidgetBase {
|
||||
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));
|
||||
// @todo Support entity types other than 'node'. Will be fixed in
|
||||
// https://www.drupal.org/node/2423093.
|
||||
if ($entity_type == 'node' && $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id)) {
|
||||
$displayable_string = EntityAutocomplete::getEntityLabels([$entity]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +154,7 @@ class LinkWidget extends WidgetBase {
|
||||
$element['title']['#required'] = TRUE;
|
||||
// We expect the field name placeholder value to be wrapped in t() here,
|
||||
// so it won't be escaped again as it's already marked safe.
|
||||
$form_state->setError($element['title'], t('@name field is required.', array('@name' => $element['title']['#title'])));
|
||||
$form_state->setError($element['title'], t('@name field is required.', ['@name' => $element['title']['#title']]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +165,7 @@ class LinkWidget extends WidgetBase {
|
||||
/** @var \Drupal\link\LinkItemInterface $item */
|
||||
$item = $items[$delta];
|
||||
|
||||
$element['uri'] = array(
|
||||
$element['uri'] = [
|
||||
'#type' => 'url',
|
||||
'#title' => $this->t('URL'),
|
||||
'#placeholder' => $this->getSetting('placeholder_url'),
|
||||
@@ -172,10 +173,10 @@ class LinkWidget extends WidgetBase {
|
||||
// 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')),
|
||||
'#element_validate' => [[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.
|
||||
@@ -193,44 +194,45 @@ class LinkWidget extends WidgetBase {
|
||||
}
|
||||
|
||||
// If the field is configured to allow only internal links, add a useful
|
||||
// element prefix.
|
||||
// element prefix and description.
|
||||
if (!$this->supportsExternalLinks()) {
|
||||
$element['uri']['#field_prefix'] = rtrim(\Drupal::url('<front>', array(), array('absolute' => TRUE)), '/');
|
||||
$element['uri']['#field_prefix'] = rtrim(\Drupal::url('<front>', [], ['absolute' => TRUE]), '/');
|
||||
$element['uri']['#description'] = $this->t('This must be an internal path such as %add-node. You can also start typing the title of a piece of content to select it. Enter %front to link to the front page.', ['%add-node' => '/node/add', '%front' => '<front>']);
|
||||
}
|
||||
// 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'));
|
||||
$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.', ['%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['uri']['#description'] = $this->t('This must be an external URL such as %url.', ['%url' => 'http://example.com']);
|
||||
}
|
||||
|
||||
$element['title'] = array(
|
||||
$element['title'] = [
|
||||
'#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');
|
||||
$element['#element_validate'][] = [get_called_class(), 'validateTitleElement'];
|
||||
}
|
||||
|
||||
// Exposing the attributes array in the widget is left for alternate and more
|
||||
// advanced field widgets.
|
||||
$element['attributes'] = array(
|
||||
$element['attributes'] = [
|
||||
'#type' => 'value',
|
||||
'#tree' => TRUE,
|
||||
'#value' => !empty($items[$delta]->options['attributes']) ? $items[$delta]->options['attributes'] : array(),
|
||||
'#attributes' => array('class' => array('link-field-widget-attributes')),
|
||||
);
|
||||
'#value' => !empty($items[$delta]->options['attributes']) ? $items[$delta]->options['attributes'] : [],
|
||||
'#attributes' => ['class' => ['link-field-widget-attributes']],
|
||||
];
|
||||
|
||||
// If cardinality is 1, ensure a proper label is output for the field.
|
||||
if ($this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1) {
|
||||
@@ -241,9 +243,9 @@ class LinkWidget extends WidgetBase {
|
||||
}
|
||||
// Otherwise wrap everything in a details element.
|
||||
else {
|
||||
$element += array(
|
||||
$element += [
|
||||
'#type' => 'fieldset',
|
||||
);
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,23 +282,23 @@ class LinkWidget extends WidgetBase {
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements = parent::settingsForm($form, $form_state);
|
||||
|
||||
$elements['placeholder_url'] = array(
|
||||
$elements['placeholder_url'] = [
|
||||
'#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(
|
||||
];
|
||||
$elements['placeholder_title'] = [
|
||||
'#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),
|
||||
),
|
||||
),
|
||||
);
|
||||
'#states' => [
|
||||
'invisible' => [
|
||||
':input[name="instance[settings][title]"]' => ['value' => DRUPAL_DISABLED],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $elements;
|
||||
}
|
||||
@@ -305,7 +307,7 @@ class LinkWidget extends WidgetBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$summary = [];
|
||||
|
||||
$placeholder_title = $this->getSetting('placeholder_title');
|
||||
$placeholder_url = $this->getSetting('placeholder_url');
|
||||
@@ -314,10 +316,10 @@ class LinkWidget extends WidgetBase {
|
||||
}
|
||||
else {
|
||||
if (!empty($placeholder_title)) {
|
||||
$summary[] = $this->t('Title placeholder: @placeholder_title', array('@placeholder_title' => $placeholder_title));
|
||||
$summary[] = $this->t('Title placeholder: @placeholder_title', ['@placeholder_title' => $placeholder_title]);
|
||||
}
|
||||
if (!empty($placeholder_url)) {
|
||||
$summary[] = $this->t('URL placeholder: @placeholder_url', array('@placeholder_url' => $placeholder_url));
|
||||
$summary[] = $this->t('URL placeholder: @placeholder_url', ['@placeholder_url' => $placeholder_url]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-18
@@ -6,20 +6,12 @@ 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;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* Validates the LinkAccess constraint.
|
||||
*/
|
||||
class LinkAccessConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* Stores the validator's state during validation.
|
||||
*
|
||||
* @var \Symfony\Component\Validator\ExecutionContextInterface
|
||||
*/
|
||||
protected $context;
|
||||
class LinkAccessConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
|
||||
|
||||
/**
|
||||
* Proxy for the current user account.
|
||||
@@ -47,13 +39,6 @@ class LinkAccessConstraintValidator implements ConstraintValidatorInterface, Con
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function initialize(ExecutionContextInterface $context) {
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -70,7 +55,7 @@ class LinkAccessConstraintValidator implements ConstraintValidatorInterface, Con
|
||||
// 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));
|
||||
$this->context->addViolation($constraint->message, ['@uri' => $value->uri]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-18
@@ -4,27 +4,12 @@ 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;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
class LinkExternalProtocolsConstraintValidator extends ConstraintValidator {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -41,7 +26,7 @@ class LinkExternalProtocolsConstraintValidator implements ConstraintValidatorInt
|
||||
}
|
||||
// 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));
|
||||
$this->context->addViolation($constraint->message, ['@uri' => $value->uri]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-18
@@ -6,27 +6,12 @@ use Symfony\Component\Routing\Exception\InvalidParameterException;
|
||||
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
class LinkNotExistingInternalConstraintValidator extends ConstraintValidator {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -59,7 +44,7 @@ class LinkNotExistingInternalConstraintValidator implements ConstraintValidatorI
|
||||
$allowed = FALSE;
|
||||
}
|
||||
if (!$allowed) {
|
||||
$this->context->addViolation($constraint->message, array('@uri' => $value->uri));
|
||||
$this->context->addViolation($constraint->message, ['@uri' => $value->uri]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@
|
||||
|
||||
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.
|
||||
@@ -15,64 +12,8 @@ use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
* label = @Translation("Link data valid for link type.", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
class LinkTypeConstraint extends Constraint implements ConstraintValidatorInterface {
|
||||
class LinkTypeConstraint extends Constraint {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\link\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\link\LinkItemInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* Constraint validator for links receiving data allowed by its settings.
|
||||
*/
|
||||
class LinkTypeConstraintValidator extends ConstraintValidator {
|
||||
|
||||
/**
|
||||
* {@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($constraint->message, ['@uri' => $link_item->uri]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\link\Plugin\migrate\field\d6;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "link",
|
||||
* core = {6},
|
||||
* type_map = {
|
||||
* "link_field" = "link"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class LinkField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
// See d6_field_formatter_settings.yml and FieldPluginBase
|
||||
// processFieldFormatter().
|
||||
return [
|
||||
'default' => 'link',
|
||||
'plain' => 'link',
|
||||
'absolute' => 'link',
|
||||
'title_plain' => 'link',
|
||||
'url' => 'link',
|
||||
'short' => 'link',
|
||||
'label' => 'link',
|
||||
'separate' => 'link_separate',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
|
||||
$process = [
|
||||
'plugin' => 'd6_field_link',
|
||||
'source' => $field_name,
|
||||
];
|
||||
$migration->mergeProcessOfProperty($field_name, $process);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\link\Plugin\migrate\field\d7;
|
||||
|
||||
use Drupal\link\Plugin\migrate\field\d6\LinkField as D6LinkField;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "link_field",
|
||||
* core = {7},
|
||||
* type_map = {
|
||||
* "link_field" = "link"
|
||||
* }
|
||||
* )
|
||||
*
|
||||
* This plugin provides the exact same functionality as the Drupal 6 "link"
|
||||
* plugin with the exception that the plugin ID "link_field" is used in the
|
||||
* field type map.
|
||||
*/
|
||||
class LinkField extends D6LinkField {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldWidgetMap() {
|
||||
// By default, use the plugin ID for the widget types.
|
||||
return ['link_field' => 'link_default'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function processFieldInstance(MigrationInterface $migration) {
|
||||
$process = [
|
||||
'plugin' => 'static_map',
|
||||
'source' => 'instance_settings/title',
|
||||
'bypass' => TRUE,
|
||||
'map' => [
|
||||
'disabled' => DRUPAL_DISABLED,
|
||||
'optional' => DRUPAL_OPTIONAL,
|
||||
'required' => DRUPAL_REQUIRED,
|
||||
],
|
||||
];
|
||||
$migration->mergeProcessOfProperty('settings/title', $process);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,85 +2,16 @@
|
||||
|
||||
namespace Drupal\link\Plugin\migrate\process\d6;
|
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
@trigger_error('CckLink is deprecated in Drupal 8.3.x and will be removed before
|
||||
Drupal 9.0.x. Use \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.',
|
||||
E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "d6_cck_link"
|
||||
* )
|
||||
*
|
||||
* @deprecated in Drupal 8.3.x, to be removed before Drupal 9.0.x. Use
|
||||
* \Drupal\link\Plugin\migrate\process\d6\FieldLink instead.
|
||||
*/
|
||||
class CckLink extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->migration = $migration;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$migration
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a Drupal 6 URI into a Drupal 8-compatible format.
|
||||
*
|
||||
* @param string $uri
|
||||
* The 'url' value from Drupal 6.
|
||||
*
|
||||
* @return string
|
||||
* The Drupal 8-compatible URI.
|
||||
*
|
||||
* @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
|
||||
*/
|
||||
protected function canonicalizeUri($uri) {
|
||||
// If we already have a scheme, we're fine.
|
||||
if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) {
|
||||
return $uri;
|
||||
}
|
||||
|
||||
// Remove the <front> component of the URL.
|
||||
if (strpos($uri, '<front>') === 0) {
|
||||
$uri = substr($uri, strlen('<front>'));
|
||||
}
|
||||
|
||||
// Add the internal: scheme and ensure a leading slash.
|
||||
return 'internal:/' . ltrim($uri, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
$attributes = unserialize($value['attributes']);
|
||||
// Drupal 6 link attributes might be double serialized.
|
||||
if (!is_array($attributes)) {
|
||||
$attributes = unserialize($attributes);
|
||||
}
|
||||
|
||||
if (!$attributes) {
|
||||
$attributes = [];
|
||||
}
|
||||
|
||||
// Massage the values into the correct form for the link.
|
||||
$route['uri'] = $this->canonicalizeUri($value['url']);
|
||||
$route['options']['attributes'] = $attributes;
|
||||
$route['title'] = $value['title'];
|
||||
return $route;
|
||||
}
|
||||
|
||||
}
|
||||
class CckLink extends FieldLink { }
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\link\Plugin\migrate\process\d6;
|
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateExecutableInterface;
|
||||
use Drupal\migrate\ProcessPluginBase;
|
||||
use Drupal\migrate\Row;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @MigrateProcessPlugin(
|
||||
* id = "d6_field_link"
|
||||
* )
|
||||
*/
|
||||
class FieldLink extends ProcessPluginBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->migration = $migration;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$migration
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a Drupal 6 URI into a Drupal 8-compatible format.
|
||||
*
|
||||
* @param string $uri
|
||||
* The 'url' value from Drupal 6.
|
||||
*
|
||||
* @return string
|
||||
* The Drupal 8-compatible URI.
|
||||
*
|
||||
* @see \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
|
||||
*/
|
||||
protected function canonicalizeUri($uri) {
|
||||
// If we already have a scheme, we're fine.
|
||||
if (empty($uri) || !is_null(parse_url($uri, PHP_URL_SCHEME))) {
|
||||
return $uri;
|
||||
}
|
||||
|
||||
// Remove the <front> component of the URL.
|
||||
if (strpos($uri, '<front>') === 0) {
|
||||
$uri = substr($uri, strlen('<front>'));
|
||||
}
|
||||
|
||||
// Add the internal: scheme and ensure a leading slash.
|
||||
return 'internal:/' . ltrim($uri, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
|
||||
$attributes = unserialize($value['attributes']);
|
||||
// Drupal 6 link attributes might be double serialized.
|
||||
if (!is_array($attributes)) {
|
||||
$attributes = unserialize($attributes);
|
||||
}
|
||||
|
||||
if (!$attributes) {
|
||||
$attributes = [];
|
||||
}
|
||||
|
||||
// Massage the values into the correct form for the link.
|
||||
$route['uri'] = $this->canonicalizeUri($value['url']);
|
||||
$route['options']['attributes'] = $attributes;
|
||||
$route['title'] = $value['title'];
|
||||
return $route;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,27 +40,27 @@ class LinkViewsTokensTest extends ViewTestBase {
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
ViewTestData::createTestViews(get_class($this), array('link_test_views'));
|
||||
ViewTestData::createTestViews(get_class($this), ['link_test_views']);
|
||||
|
||||
// Create Basic page node type.
|
||||
$this->drupalCreateContentType(array(
|
||||
$this->drupalCreateContentType([
|
||||
'type' => 'page',
|
||||
'name' => 'Basic page'
|
||||
));
|
||||
]);
|
||||
|
||||
// Create a field.
|
||||
FieldStorageConfig::create(array(
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => $this->fieldName,
|
||||
'type' => 'link',
|
||||
'entity_type' => 'node',
|
||||
'cardinality' => 1,
|
||||
))->save();
|
||||
FieldConfig::create(array(
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'field_name' => $this->fieldName,
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'label' => 'link field',
|
||||
))->save();
|
||||
])->save();
|
||||
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class LinkViewsTokensTest extends ViewTestBase {
|
||||
|
||||
// Add nodes with the URI's and titles.
|
||||
foreach ($uris as $uri => $title) {
|
||||
$values = array('type' => 'page');
|
||||
$values = ['type' => 'page'];
|
||||
$values[$this->fieldName][] = ['uri' => $uri, 'title' => $title, 'options' => ['attributes' => ['class' => 'test-link-class']]];
|
||||
$this->drupalCreateNode($values);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user