diff --git a/core/modules/link/lib/Drupal/link/LinkItemInterface.php b/core/modules/link/lib/Drupal/link/LinkItemInterface.php index bd71b41..0a1e55d 100644 --- a/core/modules/link/lib/Drupal/link/LinkItemInterface.php +++ b/core/modules/link/lib/Drupal/link/LinkItemInterface.php @@ -14,6 +14,21 @@ interface LinkItemInterface extends ConfigFieldItemInterface { /** + * Specifies whether the field supports only internal URLs. + */ + const LINK_INTERNAL = 0x01; + + /** + * Specifies whether the field supports only external URLs. + */ + const LINK_EXTERNAL = 0x10; + + /** + * Specifies whether the field supports both internal and external URLs. + */ + const LINK_GENERIC = 0x11; + + /** * Determines if a link is external. * * @return bool diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkFormatter.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkFormatter.php index 7860145..01052db 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkFormatter.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkFormatter.php @@ -180,7 +180,7 @@ protected function buildUrl(LinkItemInterface $item) { $url->setOptions($options); } else { - $url = new Url($item->route_name, (array) $item->route_paramaters, (array) $options); + $url = new Url($item->route_name, (array) $item->route_parameters, (array) $options); } return $url; diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldType/LinkItem.php index 83502b7..105a358 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldType/LinkItem.php @@ -22,7 +22,7 @@ * label = @Translation("Link"), * description = @Translation("Stores a URL string, optional varchar link text, and optional blob of options to assemble a link."), * instance_settings = { - * "url_type" = "2", + * "url_type" = \Drupal\link\LinkItemInterface::LINK_GENERIC, * "title" = "1" * }, * default_widget = "link_default", @@ -32,21 +32,6 @@ class LinkItem extends ConfigFieldItemBase implements LinkItemInterface { /** - * Specifies whether the field supports only internal URLs. - */ - const LINK_INTERNAL = 0; - - /** - * Specifies whether the field supports only external URLs. - */ - const LINK_EXTERNAL = 1; - - /** - * Specifies whether the field supports both internal and external URLs. - */ - const LINK_GENERIC = 2; - - /** * {@inheritdoc} */ public static function propertyDefinitions(FieldDefinitionInterface $field_definition) { @@ -145,7 +130,7 @@ public function instanceSettingsForm(array $form, array &$form_state) { * {@inheritdoc} */ public function preSave() { - // Trim any spaces around the URL and link text. + // Trim any spaces around the link text. $this->title = trim($this->title); // Split out the link 'query' and 'fragment' parts. diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php index f782080..4cfecef 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php @@ -13,7 +13,7 @@ use Drupal\Core\ParamConverter\ParamNotConvertedException; use Drupal\Core\Routing\MatchingRouteNotFoundException; use Drupal\Core\Url; -use Drupal\link\Plugin\Field\FieldType\LinkItem; +use Drupal\link\LinkItemInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -54,22 +54,21 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#required' => $element['#required'], ); - // If the field is configured to allow only internal or both internal and - // external paths, it cannot use the 'url' form element and we have to do - // the validation ourselves. - if ($url_type != LinkItem::LINK_EXTERNAL) { + // If the field is configured to allows internal paths, it cannot use the + // 'url' form element and we have to do the validation ourselves. + if ($url_type & LinkItemInterface::LINK_INTERNAL) { $element['url']['#type'] = 'textfield'; $element['#element_validate'][] = array($this, 'validateUrl'); } // If the field is configured to allow only internal paths, add a useful // element prefix. - if ($url_type == LinkItem::LINK_INTERNAL) { + if ($url_type == LinkItemInterface::LINK_INTERNAL) { $element['url']['#field_prefix'] = \Drupal::url('', array(), array('absolute' => TRUE)); } // If the field is configured to allow both internal and external paths, // show a useful description. - elseif ($url_type == LinkItem::LINK_GENERIC) { + elseif ($url_type == LinkItemInterface::LINK_GENERIC) { $element['url']['#description'] = $this->t('This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')); } @@ -178,15 +177,15 @@ public function validateUrl(&$element, &$form_state, $form) { $url_type = $this->getFieldSetting('url_type'); $url_is_valid = TRUE; - // Validate only if the field type supports external and/or internal URLs. - if ($element['url']['#value'] !== '' && $url_type != LinkItem::LINK_EXTERNAL) { + // Validate only if the field type supports internal URLs. + if ($element['url']['#value'] !== '' && $url_type & LinkItemInterface::LINK_INTERNAL) { try { $url = Url::createFromPath($element['url']['#value']); if ($url->isExternal() && !UrlHelper::isValid($element['url']['#value'], TRUE)) { $url_is_valid = FALSE; } - elseif ($url->isExternal() && $url_type == LinkItem::LINK_INTERNAL) { + elseif ($url->isExternal() && $url_type == LinkItemInterface::LINK_INTERNAL) { $url_is_valid = FALSE; } } diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php index a66fc95..b347a9a 100644 --- a/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php +++ b/core/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php @@ -8,7 +8,7 @@ namespace Drupal\link\Tests; use Drupal\Component\Utility\String; -use Drupal\link\Plugin\Field\FieldType\LinkItem; +use Drupal\link\LinkItemInterface; use Drupal\simpletest\WebTestBase; /** @@ -80,7 +80,7 @@ function testURLValidation() { 'bundle' => 'entity_test', 'settings' => array( 'title' => DRUPAL_DISABLED, - 'url_type' => LinkItem::LINK_GENERIC, + 'url_type' => LinkItemInterface::LINK_GENERIC, ), )); $this->instance->save(); @@ -124,18 +124,18 @@ function testURLValidation() { 'non/existing/path' ); - // Test external and internal URLs for 'url_type' = LinkItem::LINK_GENERIC. + // Test external and internal URLs for 'url_type' = LinkItemInterface::LINK_GENERIC. $this->assertValidEntries($field_name, $valid_external_entries + $valid_internal_entries); $this->assertInvalidEntries($field_name, $invalid_external_entries + $invalid_internal_entries); - // Test external URLs for 'url_type' = LinkItem::LINK_EXTERNAL. - $this->instance->settings['url_type'] = LinkItem::LINK_EXTERNAL; + // Test external URLs for 'url_type' = LinkItemInterface::LINK_EXTERNAL. + $this->instance->settings['url_type'] = LinkItemInterface::LINK_EXTERNAL; $this->instance->save(); $this->assertValidEntries($field_name, $valid_external_entries); $this->assertInvalidEntries($field_name, $valid_internal_entries + $invalid_external_entries); - // Test external URLs for 'url_type' = LinkItem::LINK_INTERNAL. - $this->instance->settings['url_type'] = LinkItem::LINK_INTERNAL; + // Test external URLs for 'url_type' = LinkItemInterface::LINK_INTERNAL. + $this->instance->settings['url_type'] = LinkItemInterface::LINK_INTERNAL; $this->instance->save(); $this->assertValidEntries($field_name, $valid_internal_entries); $this->assertInvalidEntries($field_name, $valid_external_entries + $invalid_internal_entries);