diff --git a/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php b/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php index 13d571a68f..59bf96a9f0 100644 --- a/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php +++ b/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php @@ -15,7 +15,7 @@ * label = @Translation("Link URL") * ) */ -class LinkUrlComputed extends Uri implements CacheableDependencyInterface { +class LinkUrlComputed extends Uri implements CacheableDependencyInterface { /** * The generated URL. @@ -44,6 +44,8 @@ public function getValue() { public function setValue($value, $notify = TRUE) { if (!empty($value)) { $parsed = UrlHelper::parse($value); + // If the path is not an external URL then add 'internal:' prefix to make + // it a valid uri. if (strpos($parsed['path'], ':') === FALSE) { $parsed['path'] = 'internal:' . $parsed['path']; } diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index afd83b3e59..8902180896 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -190,10 +190,13 @@ public function onChange($property_name, $notify = TRUE) { $property = $this->get('url'); if ($url = $property->getValue()) { $parsed = UrlHelper::parse($url); + // If the path is not an external URL then add 'internal:' prefix to + // make it a valid uri. if (strpos($parsed['path'], ':') === FALSE) { $parsed['path'] = 'internal:' . $parsed['path']; } $this->writePropertyValue('uri', $parsed['path']); + // Only set the options if we have query parameters or a fragment. if (!empty($parsed['query']) || !empty($parsed['fragment'])) { $this->writePropertyValue('options', ['query' => $parsed['query'], 'fragment' => $parsed['fragment']]); } @@ -223,6 +226,9 @@ public function setValue($values, $notify = TRUE) { } } parent::setValue($values, $notify); + // Support setting the field item with only url property, but make sure + // values stay in sync if only url property is passed. + // NULL is a valid value, so we use array_key_exists(). if (is_array($values) && array_key_exists('url', $values) && !array_key_exists('uri', $values)) { $this->onChange('url', FALSE); } diff --git a/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php b/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php index 039198c845..af3ded6c13 100644 --- a/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php +++ b/core/modules/menu_link_content/tests/src/Functional/Rest/MenuLinkContentResourceTestBase.php @@ -90,7 +90,7 @@ protected function getNormalizedPostEntity() { 'link' => [ [ 'uri' => 'http://www.urbandictionary.com/define.php?term=drama%20llama', - 'url' => 'http://www.urbandictionary.com/define.php?term=drama%20llama', + 'url' => 'http://www.urbandictionary.com/define.php?term=drama%20llama#a-fragment', 'options' => [ 'fragment' => 'a-fragment', 'attributes' => [