This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display a message on blog posts over a year old
References #30
- Loading branch information
Showing
7 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
old_post_message_text: '<strong>Warning:</strong> This post is over a year old. I don''t always update old posts with new information, so some of this information may be out of date.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
web/modules/custom/blog/src/EventSubscriber/DisplayMessageOnOldPosts.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\opdavies_blog\EventSubscriber; | ||
|
||
use Drupal\Component\Datetime\TimeInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Messenger\MessengerInterface; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent; | ||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface; | ||
use Drupal\opdavies_blog\Entity\Node\Post; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class DisplayMessageOnOldPosts implements EventSubscriberInterface { | ||
|
||
use StringTranslationTrait; | ||
|
||
private ConfigFactoryInterface $configFactory; | ||
|
||
private MessengerInterface $messenger; | ||
|
||
private TimeInterface $time; | ||
|
||
public function __construct( | ||
ConfigFactoryInterface $configFactory, | ||
MessengerInterface $messenger, | ||
TimeInterface $time | ||
) { | ||
$this->configFactory = $configFactory; | ||
$this->messenger = $messenger; | ||
$this->time = $time; | ||
} | ||
|
||
public function displayMessage(AbstractEntityEvent $event): void { | ||
$entity = $event->getEntity(); | ||
|
||
if ($entity->getEntityTypeId() != 'node') { | ||
return; | ||
} | ||
|
||
if ($entity->bundle() != 'post') { | ||
return; | ||
} | ||
|
||
if ($this->wasPostPublishedTooRecently($entity)) { | ||
return; | ||
} | ||
|
||
$text = $this->configFactory->get('opdavies_blog.settings') | ||
->get('old_post_message_text'); | ||
|
||
$this->messenger->addStatus($this->t($text)); | ||
} | ||
|
||
public static function getSubscribedEvents() { | ||
return [ | ||
HookEventDispatcherInterface::ENTITY_VIEW => 'displayMessage', | ||
]; | ||
} | ||
|
||
private function wasPostPublishedTooRecently(Post $post): bool { | ||
$currentTime = $this->time->getRequestTime(); | ||
$postAgeInSeconds = $currentTime - $post->getCreatedTime(); | ||
$maxAgeInSeconds = 60 * 60 * 24 * 365; | ||
|
||
return $postAgeInSeconds < $maxAgeInSeconds; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
web/modules/custom/blog/tests/src/Functional/DisplayMessageOnOldPostsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\opdavies_blog\Functional; | ||
|
||
use Carbon\Carbon; | ||
use Drupal\opdavies_blog_test\Factory\PostFactory; | ||
use Drupal\Tests\BrowserTestBase; | ||
|
||
final class DisplayMessageOnOldPostsTest extends BrowserTestBase { | ||
|
||
protected static $modules = [ | ||
'field', | ||
'link', | ||
'node', | ||
'taxonomy', | ||
|
||
'hook_event_dispatcher', | ||
'core_event_dispatcher', | ||
|
||
'opdavies_blog_test', | ||
'opdavies_blog', | ||
]; | ||
|
||
/** @test */ | ||
public function a_message_is_displayed_on_posts_over_a_year_old(): void { | ||
\Drupal::configFactory()->getEditable('opdavies_blog.settings') | ||
->set('old_post_message_text', 'This is an old post.') | ||
->save(TRUE); | ||
|
||
$post = (new PostFactory())->create([ | ||
'created' => Carbon::now()->subYear()->subDay()->getTimestamp(), | ||
]); | ||
$post->save(); | ||
|
||
$this->drupalGet('node/1'); | ||
|
||
$this->assertSession()->pageTextContainsOnce('This is an old post.'); | ||
} | ||
|
||
/** @test */ | ||
public function a_message_is_not_displayed_for_posts_less_than_a_year_old(): void { | ||
\Drupal::configFactory()->getEditable('opdavies_blog.settings') | ||
->set('old_post_message_text', 'This is an old post.') | ||
->save(TRUE); | ||
|
||
$post = (new PostFactory())->create([ | ||
'created' => Carbon::now()->subDays(364)->getTimestamp(), | ||
]); | ||
$post->save(); | ||
|
||
$this->drupalGet('node/1'); | ||
|
||
$this->assertSession()->pageTextNotContains('This is an old post.'); | ||
} | ||
|
||
/** @test */ | ||
public function a_message_is_not_displayed_for_non_post_nodes(): void { | ||
$this->drupalCreateContentType(['type' => 'page']); | ||
|
||
\Drupal::configFactory()->getEditable('opdavies_blog.settings') | ||
->set('old_post_message_text', 'This is an old post.') | ||
->save(TRUE); | ||
|
||
$this->drupalCreateNode([ | ||
'created' => Carbon::now()->subYear()->subDay()->getTimestamp(), | ||
'type' => 'page', | ||
]); | ||
|
||
$this->drupalGet('node/1'); | ||
|
||
$this->assertSession()->pageTextNotContains('This is an old post.'); | ||
} | ||
|
||
} |