From 66a15d60bf3573226849d336ecccf8cdd86b99ce Mon Sep 17 00:00:00 2001 From: Imanol Eguskiza Date: Tue, 16 Feb 2021 13:07:04 +0100 Subject: [PATCH] EWPP-776: Add atom link, replace image link and use correct date formatter. --- oe_list_pages.module | 1 + src/Controller/ListPageRssController.php | 16 +++++++++------- templates/oe-list-pages-rss.html.twig | 3 ++- .../ListPageRssControllerTest.php | 9 +++++---- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/oe_list_pages.module b/oe_list_pages.module index 4bde5547..a6000a07 100644 --- a/oe_list_pages.module +++ b/oe_list_pages.module @@ -33,6 +33,7 @@ function oe_list_pages_theme($existing, $type, $theme, $path) { 'variables' => [ 'title' => '', 'link' => '', + 'atom_link' => '', 'channel_description' => '', 'language' => '', 'copyright' => '', diff --git a/src/Controller/ListPageRssController.php b/src/Controller/ListPageRssController.php index 9e68f3f4..5a62df20 100644 --- a/src/Controller/ListPageRssController.php +++ b/src/Controller/ListPageRssController.php @@ -14,10 +14,10 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\GeneratedUrl; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Theme\ThemeManagerInterface; -use Drupal\Core\Url; use Drupal\emr\Entity\EntityMetaInterface; use Drupal\facets\UrlProcessor\UrlProcessorPluginManager; use Drupal\node\NodeInterface; @@ -194,16 +194,18 @@ public function build(NodeInterface $node): CacheableResponse { 'query' => $query, 'language' => $language, ])->toString(TRUE); + $atom_link = $this->request->getCurrentRequest()->getSchemeAndHttpHost() . $this->request->getCurrentRequest()->getRequestUri(); $cache_metadata->addCacheableDependency($default_link); $default_description = $this->getChannelDescription($node, $cache_metadata); $build = [ '#theme' => 'oe_list_pages_rss', '#title' => $default_title, '#link' => $default_link->getGeneratedUrl(), + '#atom_link' => $atom_link, '#channel_description' => empty($default_description) ? $default_title : $default_description, '#language' => $language->getId(), '#copyright' => $this->getChannelCopyright(), - '#image' => $this->getChannelImage($cache_metadata), + '#image' => $this->getChannelImage($default_link, $cache_metadata), '#channel_elements' => [], '#items' => $this->getItemList($node, $cache_metadata), ]; @@ -288,7 +290,7 @@ protected function getItemList(NodeInterface $node, CacheableMetadata $cache_met $result_item['#item_elements']['pubDate'] = [ '#type' => 'html_tag', '#tag' => 'pubDate', - '#value' => $this->dateFormatter->format($creation_date, 'custom', DateTimeInterface::RFC822), + '#value' => $this->dateFormatter->format($creation_date, 'custom', DateTimeInterface::RFC2822), ]; } @@ -340,13 +342,15 @@ protected function getChannelTitle(NodeInterface $node, CacheableMetadata $cache /** * Get the channel image array. * + * @param \Drupal\Core\GeneratedUrl $url + * The url to use. * @param \Drupal\Core\Cache\CacheableMetadata $cache_metadata * The current cache metadata. * * @return array * The image information array. */ - protected function getChannelImage(CacheableMetadata $cache_metadata): array { + protected function getChannelImage(GeneratedUrl $url, CacheableMetadata $cache_metadata): array { $site_config = $this->configFactory->get('system.site'); $cache_metadata->addCacheableDependency($site_config); $site_name = $site_config->get('name'); @@ -354,12 +358,10 @@ protected function getChannelImage(CacheableMetadata $cache_metadata): array { // Get the logo location. $theme = $this->themeManager->getActiveTheme(); $cache_metadata->addCacheContexts(['theme']); - $site_url = Url::fromRoute('', [], ['absolute' => TRUE])->toString(TRUE); - $cache_metadata->addCacheableDependency($site_url); return [ 'url' => file_create_url($theme->getLogo()), 'title' => $title, - 'link' => $site_url->getGeneratedUrl(), + 'link' => $url->getGeneratedUrl(), ]; } diff --git a/templates/oe-list-pages-rss.html.twig b/templates/oe-list-pages-rss.html.twig index f5d0a8ee..a64edf65 100644 --- a/templates/oe-list-pages-rss.html.twig +++ b/templates/oe-list-pages-rss.html.twig @@ -15,10 +15,11 @@ */ #} - + {{ title }} {{ link }} + {{ channel_description }} {{ language }} {{ copyright }} diff --git a/tests/src/FunctionalJavascript/ListPageRssControllerTest.php b/tests/src/FunctionalJavascript/ListPageRssControllerTest.php index 1bf18ce9..be742d60 100644 --- a/tests/src/FunctionalJavascript/ListPageRssControllerTest.php +++ b/tests/src/FunctionalJavascript/ListPageRssControllerTest.php @@ -148,12 +148,13 @@ public function testListPageRssPage(): void { $channel = $crawler->filterXPath('//rss[@version=2.0]/channel'); $this->assertEquals('Drupal | List page test', $channel->filterXPath('//title')->text()); $this->assertEquals('http://web:8080/build/node/1', $channel->filterXPath('//link')->text()); + $this->assertEquals('http://web:8080/build/node/1/rss', $channel->filterXPath('//atom:link')->attr('href')); $this->assertEquals('Drupal | List page test', $channel->filterXPath('//description')->text()); $this->assertEquals('en', $channel->filterXPath('//language')->text()); $this->assertEquals('© European Union, 1995-' . date('Y'), $channel->filterXPath('//copyright')->text()); $this->assertEquals('http://web:8080/build/core/themes/classy/logo.svg', $channel->filterXPath('//image/url')->text()); $this->assertEquals('Drupal logo', $channel->filterXPath('//image/title')->text()); - $this->assertEquals('http://web:8080/build/', $channel->filterXPath('//image/link')->text()); + $this->assertEquals('http://web:8080/build/node/1', $channel->filterXPath('//image/link')->text()); // Assert modules subscribing to the ListPageRssAlterEvent can // alter the build. $this->assertEquals('custom_value', $channel->filterXPath('//custom_tag')->text()); @@ -167,7 +168,7 @@ public function testListPageRssPage(): void { $this->assertEquals('<p>this is a banana</p> ', $first_item->filterXpath('//description')->html()); $this->assertEquals('http://web:8080/build/node/2', $first_item->filterXpath('//link')->text()); $this->assertEquals('http://web:8080/build/node/2', $first_item->filterXpath('//guid')->text()); - $this->assertEquals('Wed, 20 Oct 21 00:00:00 +1100', $first_item->filterXpath('//pubDate')->text()); + $this->assertEquals('Wed, 20 Oct 2021 00:00:00 +1100', $first_item->filterXpath('//pubDate')->text()); // Assert modules subscribing to the ListPageRssItemAlterEvent can // alter the item build. $this->assertEquals('20/10/2020', $first_item->filterXpath('//creationDate')->text()); @@ -177,7 +178,7 @@ public function testListPageRssPage(): void { $this->assertEquals('<p>this is a cherry</p> ', $second_item->filterXpath('//description')->html()); $this->assertEquals('http://web:8080/build/node/3', $second_item->filterXpath('//link')->text()); $this->assertEquals('http://web:8080/build/node/3', $second_item->filterXpath('//guid')->text()); - $this->assertEquals('Tue, 20 Oct 20 00:00:00 +1100', $second_item->filterXpath('//pubDate')->text()); + $this->assertEquals('Tue, 20 Oct 2020 00:00:00 +1100', $second_item->filterXpath('//pubDate')->text()); // Assert modules subscribing to the ListPageRssItemAlterEvent can // alter the item build. $this->assertEquals('20/10/2021', $second_item->filterXpath('//creationDate')->text()); @@ -232,7 +233,7 @@ public function testListPageRssPage(): void { $this->assertEquals('Drupal | List page test ES', $channel->filterXPath('//title')->text()); $this->assertEquals('http://web:8080/build/es/node/1', $channel->filterXPath('//link')->text()); $this->assertEquals('es', $channel->filterXPath('//language')->text()); - $this->assertEquals('http://web:8080/build/es', $channel->filterXPath('//image/link')->text()); + $this->assertEquals('http://web:8080/build/es/node/1', $channel->filterXPath('//image/link')->text()); } }