Save post as sent to social media

References #328
This commit is contained in:
Oliver Davies 2021-01-10 16:20:36 +00:00
parent e617e9984d
commit 790b221646

View file

@ -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 {