diff --git a/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php b/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php index 330f36bf0a..13d571a68f 100644 --- a/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php +++ b/core/modules/link/src/Plugin/DataType/LinkUrlComputed.php @@ -44,9 +44,15 @@ public function getValue() { public function setValue($value, $notify = TRUE) { if (!empty($value)) { $parsed = UrlHelper::parse($value); + if (strpos($parsed['path'], ':') === FALSE) { + $parsed['path'] = 'internal:' . $parsed['path']; + } $url = Url::fromUri($parsed['path'], ['query' => $parsed['query'], 'fragment' => $parsed['fragment']]); $this->processed = $url->toString(TRUE); } + else { + $this->processed = NULL; + } // Notify the parent of any changes. if ($notify && isset($this->parent)) { $this->parent->onChange($this->name); diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index dd72ad6575..afd83b3e59 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -190,8 +190,13 @@ public function onChange($property_name, $notify = TRUE) { $property = $this->get('url'); if ($url = $property->getValue()) { $parsed = UrlHelper::parse($url); + if (strpos($parsed['path'], ':') === FALSE) { + $parsed['path'] = 'internal:' . $parsed['path']; + } $this->writePropertyValue('uri', $parsed['path']); - $this->writePropertyValue('options', ['query' => $parsed['query'], 'fragment' => $parsed['fragment']]); + if (!empty($parsed['query']) || !empty($parsed['fragment'])) { + $this->writePropertyValue('options', ['query' => $parsed['query'], 'fragment' => $parsed['fragment']]); + } } } parent::onChange($property_name, $notify); @@ -218,6 +223,9 @@ public function setValue($values, $notify = TRUE) { } } parent::setValue($values, $notify); + if (is_array($values) && array_key_exists('url', $values) && !array_key_exists('uri', $values)) { + $this->onChange('url', FALSE); + } } } diff --git a/core/modules/link/tests/src/Kernel/LinkItemTest.php b/core/modules/link/tests/src/Kernel/LinkItemTest.php index ea5d1b28a2..ad00a90fd4 100644 --- a/core/modules/link/tests/src/Kernel/LinkItemTest.php +++ b/core/modules/link/tests/src/Kernel/LinkItemTest.php @@ -119,24 +119,26 @@ public function testLinkItem() { $new_class = $this->randomMachineName(); $entity->field_test->uri = $new_url; $entity->field_test->title = $new_title; - $entity->field_test->first()->get('options')->set('query', NULL); + $entity->field_test->first()->get('options')->set('query', []); $entity->field_test->first()->get('options')->set('attributes', ['class' => $new_class]); $this->assertEqual($entity->field_test->uri, $new_url); $this->assertEqual($entity->field_test->title, $new_title); $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); - $this->assertNull($entity->field_test->options['query']); + $this->assertEmpty($entity->field_test->options['query']); // Read changed entity and assert changed values. $entity->save(); $entity = EntityTest::load($id); $this->assertEqual($entity->field_test->uri, $new_url); + $this->assertEqual($entity->field_test->url, $new_url); $this->assertEqual($entity->field_test->title, $new_title); $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class); - // Check that if we only set uri the default values for title and options - // are also initialized. + // Check that if we only set uri the default values for url, title, and + // options are also initialized. $entity->field_test = ['uri' => 'internal:/node/add']; $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); + $this->assertEqual($entity->field_test->url, '/node/add'); $this->assertNull($entity->field_test->title); $this->assertIdentical($entity->field_test->options, []); @@ -147,6 +149,7 @@ public function testLinkItem() { 'options' => ['query' => NULL], ]; $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); + $this->assertEqual($entity->field_test->url, '/node/add'); $this->assertNull($entity->field_test->title); $this->assertNull($entity->field_test->options['query']); @@ -199,6 +202,13 @@ public function testLinkItem() { $this->assertEquals($entity->field_test[0]->uri, $parsed_url['path']); $this->assertEquals($entity->field_test->options['query'], $parsed_url['query']); $this->assertEquals($entity->field_test->options['fragment'], $parsed_url['fragment']); + + // Check that if we only set url the default values for uri, title, and + // options are also initialized. + $entity->field_test = ['url' => '/node/add']; + $this->assertEqual($entity->field_test->uri, 'internal:/node/add'); + $this->assertEqual($entity->field_test->url, '/node/add'); + $this->assertNull($entity->field_test->title); } /**