From 790b221646ed45d6c69781a0cde6b0e6b1a1ff97 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Sun, 10 Jan 2021 16:20:36 +0000 Subject: [PATCH] Save post as sent to social media References #328 --- .../QueueWorker/PostPusherQueueWorker.php | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/blog/src/Plugin/QueueWorker/PostPusherQueueWorker.php b/web/modules/custom/blog/src/Plugin/QueueWorker/PostPusherQueueWorker.php index 86c3e54..0bd6244 100644 --- a/web/modules/custom/blog/src/Plugin/QueueWorker/PostPusherQueueWorker.php +++ b/web/modules/custom/blog/src/Plugin/QueueWorker/PostPusherQueueWorker.php @@ -4,8 +4,11 @@ declare(strict_types=1); namespace Drupal\opdavies_blog\Plugin\QueueWorker; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueWorkerBase; use Drupal\opdavies_blog\Entity\Node\Post; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * @QueueWorker( @@ -14,14 +17,55 @@ use Drupal\opdavies_blog\Entity\Node\Post; * cron = {"time": 30} * ) */ -final class PostPusherQueueWorker extends QueueWorkerBase { +final class PostPusherQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface { + + private EntityStorageInterface $nodeStorage; + + public function __construct( + array $configuration, + string $pluginId, + array $pluginDefinition, + EntityStorageInterface $nodeStorage + ) { + parent::__construct($configuration, $pluginId, $pluginDefinition); + + $this->nodeStorage = $nodeStorage; + } + + public static function create( + ContainerInterface $container, + array $configuration, + $pluginId, + $pluginDefinition + ) { + return new static( + $configuration, + $pluginId, + $pluginDefinition, + $container->get('entity_type.manager')->getStorage('node') + ); + } public function processItem($data): void { + /** @var Post $post */ ['post' => $post] = $data; if (!$this->shouldBePushed($post)) { return; } + + if (!$post->isLatestRevision()) { + $post = $this->nodeStorage->load($post->id()); + + // @phpstan-ignore-next-line + if (!$this->shouldBePushed($post)) { + return; + } + } + + // @phpstan-ignore-next-line + $post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); + $post->save(); } private function shouldBePushed(Post $post): bool {