From b98d181a58a468ce7a54bcc4ba838625f9bb28c1 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 19 Mar 2020 22:01:10 +0000 Subject: [PATCH] 5c: Add isPublishable method to article wrapper Add the `isPublishable()` method to the article wrapper. This contains all of the logic that is used to determine if an article in publishable or not, and returns a boolean. --- .../custom/my_module/src/Wrapper/ArticleWrapper.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/my_module/src/Wrapper/ArticleWrapper.php b/web/modules/custom/my_module/src/Wrapper/ArticleWrapper.php index db8508e..1a4b02c 100644 --- a/web/modules/custom/my_module/src/Wrapper/ArticleWrapper.php +++ b/web/modules/custom/my_module/src/Wrapper/ArticleWrapper.php @@ -2,15 +2,18 @@ namespace Drupal\my_module\Wrapper; +use Drupal\Component\Datetime\TimeInterface; use Drupal\node\NodeInterface; class ArticleWrapper { private $article; + private $time; - public function __construct(NodeInterface $node) { + public function __construct(TimeInterface $time, NodeInterface $node) { $this->verifyNodeType($node); + $this->time = $time; $this->article = $node; } @@ -18,6 +21,13 @@ class ArticleWrapper { return $this->article; } + public function isPublishable(): bool { + $created = $this->article->getCreatedTime(); + $difference = $this->time->getRequestTime() - $created; + + return $difference >= 60 * 60 * 24 * 3; + } + private function verifyNodeType(NodeInterface $node): void { if ($node->bundle() != 'article') { throw new \InvalidArgumentException(sprintf(