client = $client; $this->config = $configFactory->get('opdavies_blog.settings'); } /** * @inheritDoc */ public static function getSubscribedEvents() { return [ HookEventDispatcherInterface::ENTITY_INSERT => 'onEntityUpdate', HookEventDispatcherInterface::ENTITY_UPDATE => 'onEntityUpdate', ]; } public function onEntityUpdate(AbstractEntityEvent $event): void { $entity = $event->getEntity(); if ($entity->getEntityTypeId() != 'node') { return; } /** @var Post $entity */ if ($entity->bundle() != 'post') { return; } if (!$entity->isPublished()) { return; } if (!$entity->shouldSendToSocialMedia()) { return; } // If this post has already been sent to social media, do not send it again. if ($entity->hasBeenSentToSocialMedia()) { return; } if ($entity->isExternalPost()) { return; } if (!$url = $this->config->get('zapier_post_tweet_url')) { return; } $this->client->post($url, [ 'form_params' => [ 'message' => $entity->toTweet(), ], ]); $entity->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE); $entity->save(); } }