diff --git a/core/modules/link/src/LinkItemInterface.php b/core/modules/link/src/LinkItemInterface.php index 530617044832216bd7d171016aec47703ecc558a..7d40c9dca45855a8712f9b9f74129f7dc8ca7717 100644 --- a/core/modules/link/src/LinkItemInterface.php +++ b/core/modules/link/src/LinkItemInterface.php @@ -40,4 +40,12 @@ public function isExternal(); */ public function getUrl(); + /** + * Gets the link title. + * + * @return string|null + * Returns the link title. + */ + public function getTitle(): ?string; + } diff --git a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php index 9154eee1cd8a3c390f14daccae55af1962be895c..2579dcbf9e05e9d64ca7c5c1f59e79c96003d460 100644 --- a/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php +++ b/core/modules/link/src/Plugin/Field/FieldType/LinkItem.php @@ -175,6 +175,13 @@ public function getUrl() { return Url::fromUri($this->uri, (array) $this->options); } + /** + * {@inheritdoc} + */ + public function getTitle(): ?string { + return $this->title ?: NULL; + } + /** * {@inheritdoc} */ diff --git a/core/modules/link/tests/src/Kernel/LinkItemTest.php b/core/modules/link/tests/src/Kernel/LinkItemTest.php index 5f4d0819b41e4e70785f2d8d74eecf95480021c4..945768d0faad747a1d8a746be653c03a3bffa575 100644 --- a/core/modules/link/tests/src/Kernel/LinkItemTest.php +++ b/core/modules/link/tests/src/Kernel/LinkItemTest.php @@ -101,6 +101,7 @@ public function testLinkItem() { $this->assertEquals($parsed_url['path'], $entity->field_test[0]->uri); $this->assertEquals($title, $entity->field_test->title); $this->assertEquals($title, $entity->field_test[0]->title); + $this->assertEquals($title, $entity->field_test[0]->getTitle()); $this->assertEquals($class, $entity->field_test->options['attributes']['class']); $this->assertEquals($parsed_url['query'], $entity->field_test->options['query']);