diff --git a/core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php b/core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php index bc03b87..4e9dc9f 100644 --- a/core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php +++ b/core/modules/aggregator/src/Plugin/Field/FieldFormatter/AggregatorTitleFormatter.php @@ -19,7 +19,7 @@ * @FieldFormatter( * id = "aggregator_title", * label = @Translation("Aggregator item title"), - * description = @Translation("Formats the aggregator item title with an optional link."), + * description = @Translation("Formats the aggregator item and aggregator feed title with an optional link."), * field_types = { * "string" * } @@ -58,18 +58,18 @@ public function viewElements(FieldItemListInterface $items) { $elements = []; if ($items->getEntity()->getEntityTypeId() == 'aggregator_feed') { - $url = Url::fromUri($items->getEntity()->getUrl()); + $url_string = $items->getEntity()->getUrl(); } else { - $url = Url::fromUri($items->getEntity()->getLink()); + $url_string = $items->getEntity()->getLink(); } - + foreach ($items as $delta => $item) { - if ($this->getSetting('display_as_link')) { + if ($this->getSetting('display_as_link') && $url_string) { $elements[$delta] = [ '#type' => 'link', '#title' => $item->value, - '#url' => $url, + '#url' => Url::fromUri($url_string), ]; } else { diff --git a/core/modules/aggregator/src/Tests/AggregatorTitleTest.php b/core/modules/aggregator/src/Tests/AggregatorTitleTest.php index e38ca1d..a35a834 100644 --- a/core/modules/aggregator/src/Tests/AggregatorTitleTest.php +++ b/core/modules/aggregator/src/Tests/AggregatorTitleTest.php @@ -50,12 +50,14 @@ protected function setUp() { * Tests the formatter output. */ public function testStringFormatter() { + // Creates an aggregator feed. $aggregator_feed = Feed::create([ 'title' => 'testing title', 'url' => 'http://www.example.com', ]); $aggregator_feed->save(); + // Creates an aggregator feed item. $aggregator_item = Item::create([ 'title' => 'test title', 'fid' => $aggregator_feed->id(), @@ -63,6 +65,7 @@ public function testStringFormatter() { ]); $aggregator_item->save(); + // Verifies aggregator feed title with and without links. $build = $aggregator_feed->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' => TRUE]]); $result = $this->render($build); @@ -74,6 +77,7 @@ public function testStringFormatter() { $this->assertTrue(strpos($result, 'testing title') === 0); $this->assertTrue(strpos($result, $aggregator_feed->getUrl()) === FALSE); + //Verifies aggregator item title with and without links. $build = $aggregator_item->{$this->fieldName}->view(['type' => 'aggregator_title', 'settings' => ['display_as_link' =>TRUE]]); $result = $this->render($build);