Skip to content

Commit

Permalink
Merge pull request #83 from openeuropa/EWPP-776
Browse files Browse the repository at this point in the history
EWPP-776: Add atom link, replace image link and use correct date formatter.
  • Loading branch information
upchuk authored Feb 19, 2021
2 parents b1d01eb + 66a15d6 commit fc52319
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions oe_list_pages.module
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function oe_list_pages_theme($existing, $type, $theme, $path) {
'variables' => [
'title' => '',
'link' => '',
'atom_link' => '',
'channel_description' => '',
'language' => '',
'copyright' => '',
Expand Down
16 changes: 9 additions & 7 deletions src/Controller/ListPageRssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
];
Expand Down Expand Up @@ -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),
];
}

Expand Down Expand Up @@ -340,26 +342,26 @@ 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');
$title = $this->t('@site_name logo', ['@site_name' => $site_name]);
// Get the logo location.
$theme = $this->themeManager->getActiveTheme();
$cache_metadata->addCacheContexts(['theme']);
$site_url = Url::fromRoute('<front>', [], ['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(),
];
}

Expand Down
3 changes: 2 additions & 1 deletion templates/oe-list-pages-rss.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
#}
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ title }}</title>
<link>{{ link }}</link>
<atom:link href="{{ atom_link }}" rel="self" type="application/rss+xml" />
<description>{{ channel_description }}</description>
<language>{{ language }}</language>
<copyright>{{ copyright }}</copyright>
Expand Down
9 changes: 5 additions & 4 deletions tests/src/FunctionalJavascript/ListPageRssControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -167,7 +168,7 @@ public function testListPageRssPage(): void {
$this->assertEquals('&lt;p&gt;this is a banana&lt;/p&gt; ', $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());
Expand All @@ -177,7 +178,7 @@ public function testListPageRssPage(): void {
$this->assertEquals('&lt;p&gt;this is a cherry&lt;/p&gt; ', $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());
Expand Down Expand Up @@ -234,7 +235,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());
}

}

0 comments on commit fc52319

Please sign in to comment.