Skip to content

Commit

Permalink
Merge pull request #716 from City-of-Helsinki/UHF-10614
Browse files Browse the repository at this point in the history
UHF-10614: News article and news item publish date changes
  • Loading branch information
teroelonen authored Oct 3, 2024
2 parents c5ecff3 + 07ef249 commit fb71750
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ the links are defined there for each display.

_News update_ is used on updating news items, and it can be referred to the `field_news_item_updating_news` field. This
is the only place it can be used, and it essentially contains the information of one update on an ongoing news story.
For example, if there is an updating news story about a soccer match, a new goal would be one news update.
For example, if there is an updating news story about a soccer match, a new goal would be one news update. Every time
a new update is added the news item's publishing time is updated to use the latest updates time so that the news item
remains on the top of time based listings while updating. Because of this the publishing time on the node page is
altered to display the oldest news update time as the publishing time to give the readers more correct information. The
other views of the news such as the teaser still use the news item's normal publishing time however.

#### Top news (front_page_top_news)

Expand Down
15 changes: 15 additions & 0 deletions public/modules/custom/helfi_etusivu/helfi_etusivu.module
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,18 @@ function helfi_etusivu_entity_bundle_info_alter(array &$bundles): void {
function helfi_etusivu_first_paragraph_grey_alter(array &$paragraphs): void {
$paragraphs[] = 'news_archive';
}

/**
* Implements hook_preprocess().
*/
function helfi_etusivu_preprocess_node__news_item__full(array &$variables): void {
$node = $variables['node'];
assert($node instanceof NewsItem);
$updating_news_publish_date = $node->getFirstUpdatingNewsPublishDate();

// Change the published_at variable only if
// there is updating news on the news item.
if (!is_null($updating_news_publish_date)) {
$variables['published_at'] = $updating_news_publish_date;
}
}
23 changes: 23 additions & 0 deletions public/modules/custom/helfi_etusivu/src/Entity/Node/NewsItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,27 @@ private function resetRadioactivity() : void {
}
}

/**
* Gets the first updating news items publish date timestamp.
*
* @return int|null
* The timestamp or null.
*/
public function getFirstUpdatingNewsPublishDate() : ?int {
$newsUpdates = $this->getNewsUpdates();

if ($first = reset($newsUpdates)) {
assert($first instanceof FieldableEntityInterface);

// PHPStan does not like $date property:
// https://www.drupal.org/project/drupal/issues/3425302.
// @phpstan-ignore-next-line
$updateDate = $first->get('field_news_update_date')->date->getTimestamp();

return $updateDate;
}

return NULL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ function helfi_node_news_article_theme() : array {
'title' => NULL,
'description' => NULL,
'image' => NULL,
'published_time' => NULL,
'html_published_time' => NULL,
'updated_time' => NULL,
'html_updated_time' => NULL,
],
],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,45 @@
* A bundle class for News article -node.
*/
final class NewsArticle extends RecommendableBase {

/**
* Get human-readable "published at" time of the News article.
*
* @return string
* The human-readable "published at" time.
*/
public function getPublishedHumanReadable(): string {
return \Drupal::service('date.formatter')->format($this->get('published_at')->value, 'publication_date_format');
}

/**
* Get machine-readable "published at" time of the News article.
*
* @return string
* The machine-readabe "published at" time.
*/
public function getPublishedMachineReadable(): string {
return \Drupal::service('date.formatter')->format($this->get('published_at')->value, 'custom', 'Y-m-d\TH:i');
}

/**
* Get human-readable "updated" time of the News article.
*
* @return string
* The human-readable "updated at" time.
*/
public function getUpdatedHumanReadable(): string {
return \Drupal::service('date.formatter')->format($this->get('changed')->value, 'publication_date_format');
}

/**
* Get machine-readable "updated" time of the News article.
*
* @return string
* The machine-readabe "updated" time.
*/
public function getUpdatedMachineReadable(): string {
return \Drupal::service('date.formatter')->format($this->get('changed')->value, 'custom', 'Y-m-d\TH:i');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\helfi_node_news_article\Plugin\Block;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\helfi_node_news_article\Entity\Node\NewsArticle;
use Drupal\helfi_platform_config\Plugin\Block\ContentBlockBase;

/**
Expand All @@ -26,6 +27,11 @@ public function build() : array {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
['entity' => $entity] = $this->getCurrentEntityVersion();

// No need to continue if current entity is not an instance of News article.
if (!$entity instanceof NewsArticle) {
return $build;
}

// No need to continue if current entity doesn't have hero field.
if (
!$entity instanceof ContentEntityInterface ||
Expand Down Expand Up @@ -69,6 +75,10 @@ public function build() : array {
'#description' => $entity->get('field_lead_in')?->first()?->view(),
'#design' => $entity->get('field_hero_design')?->first()?->getString(),
'#image' => $image,
'#published_time' => $entity->getPublishedHumanReadable(),
'#html_published_time' => $entity->getPublishedMachineReadable(),
'#updated_time' => $entity->getUpdatedHumanReadable(),
'#html_updated_time' => $entity->getUpdatedMachineReadable(),
'#cache' => [
'tags' => $entity->getCacheTags(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
title: title,
description: lead_in,
image: image,
published_time: published_time,
html_published_time: html_published_time,
updated_time: updated_time,
html_updated_time: html_updated_time,
} %}
{% endembed %}
Loading

0 comments on commit fb71750

Please sign in to comment.