diff --git a/core/lib/Drupal/Core/Url.php b/core/lib/Drupal/Core/Url.php index aea0bb5..6774d3c 100644 --- a/core/lib/Drupal/Core/Url.php +++ b/core/lib/Drupal/Core/Url.php @@ -291,6 +291,15 @@ public function setOption($name, $value) { } /** + * Returns the path of the URL if it is external. + * + * @return string + */ + public function getPath() { + return $this->path; + } + + /** * Sets the absolute value for this Url. * * @param bool $absolute @@ -308,10 +317,10 @@ public function setAbsolute($absolute = TRUE) { */ public function toString() { if ($this->isExternal()) { - return $this->urlGenerator()->generateFromPath($this->path, $this->getOptions()); + return $this->urlGenerator()->generateFromPath($this->path, $this->options); } - return $this->urlGenerator()->generateFromRoute($this->getRouteName(), $this->getRouteParameters(), $this->getOptions()); + return $this->urlGenerator()->generateFromRoute($this->routeName, $this->routeParameters, $this->options); } /** 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 d29bfbd..226d7a7 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 @@ -121,7 +121,8 @@ public function viewElements(FieldItemListInterface $items) { foreach ($items as $delta => $item) { // By default use the full URL as the link text. - $link_title = $item->url; + $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)) { @@ -144,7 +145,7 @@ public function viewElements(FieldItemListInterface $items) { $element[$delta] = array( '#type' => 'link', '#title' => $link_title, - ) + $this->buildUrl($item)->toRenderArray(); + ) + $url->toRenderArray(); } } diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php index f3417a8..9ac9282 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php @@ -42,7 +42,8 @@ public function viewElements(FieldItemListInterface $items) { foreach ($items as $delta => $item) { // By default use the full URL as the link text. - $link_title = $item->url; + $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)) { @@ -58,18 +59,17 @@ public function viewElements(FieldItemListInterface $items) { if (empty($item->title)) { $link_title = NULL; } - $url_title = $item->url; + $url_title = $url->toString(); if (!empty($settings['trim_length'])) { $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE); - $url_title = truncate_utf8($item->url, $settings['trim_length'], FALSE, TRUE); + $url_title = truncate_utf8($url_title, $settings['trim_length'], FALSE, TRUE); } - $element[$delta] = array( '#theme' => 'link_formatter_link_separate', '#title' => $link_title, '#url_title' => $url_title, - '#url' => $this->buildUrl($item), + '#url' => $url, ); } return $element; 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 b9e9aad..e133b34 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 @@ -153,11 +153,11 @@ public function instanceSettingsForm(array $form, array &$form_state) { */ public function preSave() { // Trim any spaces around the URL and link text. - $this->url = trim($this->url); $this->title = trim($this->title); // Split out the link 'query' and 'fragment' parts. - $parsed_url = UrlHelper::parse($this->url); + $parsed_url = UrlHelper::parse(trim($this->url)); + $this->url = $parsed_url['path']; $this->options = array( 'query' => $parsed_url['query'], 'fragment' => $parsed_url['fragment'], 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 bf80a66..ecf1cd5 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 @@ -36,11 +36,17 @@ class LinkWidget extends WidgetBase { public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) { $url_type = $this->getFieldSetting('url_type'); + $default_url_value = NULL; + if (isset($items[$delta]->url)) { + $url = Url::createFromPath($items[$delta]->url); + $url->setOptions($items[$delta]->options); + $default_url_value = $url->toString(); + } $element['url'] = array( '#type' => 'url', '#title' => t('URL'), '#placeholder' => $this->getSetting('placeholder_url'), - '#default_value' => isset($items[$delta]->url) ? $items[$delta]->url : NULL, + '#default_value' => $default_url_value, '#maxlength' => 2048, '#required' => $element['#required'], ); @@ -169,16 +175,16 @@ public function validateUrl(&$element, &$form_state, $form) { $url_type = $this->getFieldSetting('url_type'); // Validate only if the field type supports external and/or internal URLs. - if ($url_type != LinkItem::LINK_EXTERNAL) { + if ($element['url']['#value'] !== '' && $url_type != LinkItem::LINK_EXTERNAL) { try { $url = Url::createFromPath($element['url']['#value']); - if ($url->isExternal() && !UrlHelper::isValid($element['url']['#value'])) { - form_error($element['url'], $form_state, t('%url is not a valid external url.', array('%url' => $element['url']['#value']))); + if ($url->isExternal() && !UrlHelper::isValid($element['url']['#value'], TRUE)) { + form_error($element['url'], $form_state, t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); } } catch (\Exception $e) { - form_error($element['url'], $form_state, t('%url is not a valid internal url.', array('%url' => $element['url']['#value']))); + form_error($element['url'], $form_state, t('The URL %url is not valid.', array('%url' => $element['url']['#value']))); } } } diff --git a/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php b/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php index 4998e45..07ff6a3 100644 --- a/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php +++ b/core/modules/link/lib/Drupal/link/Tests/LinkItemTest.php @@ -58,7 +58,7 @@ public function testLinkItem() { $class = $this->randomName(); $entity->field_test->url = $url; $entity->field_test->title = $title; - $entity->field_test->first()->get('attributes')->set('class', $class); + $entity->field_test->first()->get('options')->set('attributes', array('class' => $class)); $entity->name->value = $this->randomName(); $entity->save(); @@ -71,7 +71,7 @@ public function testLinkItem() { $this->assertEqual($entity->field_test[0]->url, $url); $this->assertEqual($entity->field_test->title, $title); $this->assertEqual($entity->field_test[0]->title, $title); - $this->assertEqual($entity->field_test->attributes['class'], $class); + $this->assertEqual($entity->field_test->options['attributes']['class'], $class); // Verify changing the field value. $new_url = 'http://drupal.org'; @@ -79,17 +79,17 @@ public function testLinkItem() { $new_class = $this->randomName(); $entity->field_test->url = $new_url; $entity->field_test->title = $new_title; - $entity->field_test->first()->get('attributes')->set('class', $new_class); + $entity->field_test->first()->get('options')->set('attributes', array('class' => $new_class)); $this->assertEqual($entity->field_test->url, $new_url); $this->assertEqual($entity->field_test->title, $new_title); - $this->assertEqual($entity->field_test->attributes['class'], $new_class); + $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); // Read changed entity and assert changed values. $entity->save(); $entity = entity_load('entity_test', $id); $this->assertEqual($entity->field_test->url, $new_url); $this->assertEqual($entity->field_test->title, $new_title); - $this->assertEqual($entity->field_test->attributes['class'], $new_class); + $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); } } diff --git a/core/modules/link/link.module b/core/modules/link/link.module index 4dee62a..2c06c5d 100644 --- a/core/modules/link/link.module +++ b/core/modules/link/link.module @@ -68,6 +68,6 @@ function template_preprocess_link_formatter_link_separate(&$variables) { $variables['link'] = \Drupal::linkGenerator()->generateFromUrl($variables['url_title'], $variables['url']); } else { - $variables['link'] = l($variables['url_title'], $variables['url']->toString(), $variables['url']->getOptions()); + $variables['link'] = l($variables['url_title'], $variables['url']->getPath(), $variables['url']->getOptions()); } } diff --git a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php index ac867c3..54bbdaf 100644 --- a/core/tests/Drupal/Tests/Core/ExternalUrlTest.php +++ b/core/tests/Drupal/Tests/Core/ExternalUrlTest.php @@ -142,7 +142,7 @@ public function testToString(Url $url) { */ public function testToArray(Url $url) { $expected = array( - 'route_name' => '', + 'route_name' => NULL, 'route_parameters' => array(), 'options' => array(), ); @@ -157,7 +157,7 @@ public function testToArray(Url $url) { * @covers ::getRouteName() */ public function testGetRouteName(Url $url) { - $this->assertSame('', $url->getRouteName()); + $this->assertSame(NULL, $url->getRouteName()); } /**