Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Display a message on blog posts over a year old
Browse files Browse the repository at this point in the history
References #30
  • Loading branch information
opdavies committed Nov 2, 2020
1 parent 9ea86e6 commit a47d480
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/opdavies_blog.settings.yml
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.'
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ini name="error_reporting" value="32767"/>
<ini name="memory_limit" value="-1"/>
<env name="SIMPLETEST_BASE_URL" value="https://localhost"/>
<env name="SIMPLETEST_DB" value="sqlite://localhost/:memory:"/>
<env name="SIMPLETEST_DB" value="sqlite://localhost//tmp/.test.sqlite"/>
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
<env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
<env name="MINK_DRIVER_CLASS" value=''/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ opdavies_blog.settings:
type: config_object
label: 'Blog module configuration'
mapping:
old_post_message_text:
type: string
zapier_post_tweet_url:
type: string
5 changes: 5 additions & 0 deletions web/modules/custom/blog/opdavies_blog.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
Drupal\opdavies_blog\EventSubscriber\DisplayMessageOnOldPosts:
autowire: true
tags:
- { name: event_subscriber }

Drupal\opdavies_blog\EventSubscriber\PushBlogPostToSocialMedia:
autowire: true
tags:
Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ name: Oliver Davies Posts Test
type: module
core_version_requirement: ^8 || ^9
hidden: true
dependencies:
- drupal:node
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.');
}

}

0 comments on commit a47d480

Please sign in to comment.