From 8385d6fef701b7cce35f6b52ca58c8f46c978a6b Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 12 Aug 2020 20:28:19 +0100 Subject: [PATCH] Don't send posts to social media multiple times Check if a post has previously been sent to social media, by checking the value of a `field_sent_to_social_media` field. This field is hidden on the node add/edit forms, and populated when a post is sent to social media. Once this happens, it will not be sent to social media again. This change also populates the field for all existing posts, so that they won't be re-sent to social media either. --- ....entity_form_display.node.post.default.yml | 4 ++- ....entity_view_display.node.post.default.yml | 11 ++++++++ ...e.entity_view_display.node.post.teaser.yml | 2 ++ ...d.node.post.field_sent_to_social_media.yml | 23 ++++++++++++++++ ...torage.node.field_sent_to_social_media.yml | 18 +++++++++++++ web/modules/custom/custom/custom.install | 17 ++++++++++++ web/modules/custom/custom/custom.services.yml | 3 +++ .../custom/custom/src/Entity/Node/Post.php | 4 +++ .../PushBlogPostToSocialMedia.php | 7 ++++- .../custom/src/Repository/PostRepository.php | 27 +++++++++++++++++++ 10 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 config/default/field.field.node.post.field_sent_to_social_media.yml create mode 100644 config/default/field.storage.node.field_sent_to_social_media.yml create mode 100644 web/modules/custom/custom/custom.install create mode 100644 web/modules/custom/custom/src/Repository/PostRepository.php diff --git a/config/default/core.entity_form_display.node.post.default.yml b/config/default/core.entity_form_display.node.post.default.yml index cae82ea15..99bc3fc24 100644 --- a/config/default/core.entity_form_display.node.post.default.yml +++ b/config/default/core.entity_form_display.node.post.default.yml @@ -7,6 +7,7 @@ dependencies: - field.field.node.post.field_excerpt - field.field.node.post.field_has_tweet - field.field.node.post.field_images + - field.field.node.post.field_sent_to_social_media - field.field.node.post.field_series - field.field.node.post.field_tags - node.type.post @@ -127,4 +128,5 @@ content: region: content settings: { } third_party_settings: { } -hidden: { } +hidden: + field_sent_to_social_media: true diff --git a/config/default/core.entity_view_display.node.post.default.yml b/config/default/core.entity_view_display.node.post.default.yml index 4aa2564d4..bad63da17 100644 --- a/config/default/core.entity_view_display.node.post.default.yml +++ b/config/default/core.entity_view_display.node.post.default.yml @@ -7,6 +7,7 @@ dependencies: - field.field.node.post.field_excerpt - field.field.node.post.field_has_tweet - field.field.node.post.field_images + - field.field.node.post.field_sent_to_social_media - field.field.node.post.field_series - field.field.node.post.field_tags - node.type.post @@ -25,6 +26,16 @@ content: settings: { } third_party_settings: { } region: content + field_sent_to_social_media: + weight: 3 + label: above + settings: + format: default + format_custom_false: '' + format_custom_true: '' + third_party_settings: { } + type: boolean + region: content field_tags: weight: 2 label: above diff --git a/config/default/core.entity_view_display.node.post.teaser.yml b/config/default/core.entity_view_display.node.post.teaser.yml index dfd5af73c..6c4f01b15 100644 --- a/config/default/core.entity_view_display.node.post.teaser.yml +++ b/config/default/core.entity_view_display.node.post.teaser.yml @@ -8,6 +8,7 @@ dependencies: - field.field.node.post.field_excerpt - field.field.node.post.field_has_tweet - field.field.node.post.field_images + - field.field.node.post.field_sent_to_social_media - field.field.node.post.field_series - field.field.node.post.field_tags - node.type.post @@ -34,5 +35,6 @@ hidden: body: true field_has_tweet: true field_images: true + field_sent_to_social_media: true field_series: true field_tags: true diff --git a/config/default/field.field.node.post.field_sent_to_social_media.yml b/config/default/field.field.node.post.field_sent_to_social_media.yml new file mode 100644 index 000000000..99c581187 --- /dev/null +++ b/config/default/field.field.node.post.field_sent_to_social_media.yml @@ -0,0 +1,23 @@ +uuid: 4f639dd0-6634-43ea-89a8-141dfde87059 +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_sent_to_social_media + - node.type.post +id: node.post.field_sent_to_social_media +field_name: field_sent_to_social_media +entity_type: node +bundle: post +label: 'Sent to social media' +description: '' +required: false +translatable: false +default_value: + - + value: 0 +default_value_callback: '' +settings: + on_label: 'On' + off_label: 'Off' +field_type: boolean diff --git a/config/default/field.storage.node.field_sent_to_social_media.yml b/config/default/field.storage.node.field_sent_to_social_media.yml new file mode 100644 index 000000000..ec314a31d --- /dev/null +++ b/config/default/field.storage.node.field_sent_to_social_media.yml @@ -0,0 +1,18 @@ +uuid: 2fead662-4d91-4788-b739-8e1a56d236af +langcode: en +status: true +dependencies: + module: + - node +id: node.field_sent_to_social_media +field_name: field_sent_to_social_media +entity_type: node +type: boolean +settings: { } +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/web/modules/custom/custom/custom.install b/web/modules/custom/custom/custom.install new file mode 100644 index 000000000..7965eb774 --- /dev/null +++ b/web/modules/custom/custom/custom.install @@ -0,0 +1,17 @@ +getAll(); + + foreach ($posts as $post) { + $post->set('field_sent_to_social_media', TRUE); + $post->save(); + } +} diff --git a/web/modules/custom/custom/custom.services.yml b/web/modules/custom/custom/custom.services.yml index c553e28a4..21c4bb30a 100644 --- a/web/modules/custom/custom/custom.services.yml +++ b/web/modules/custom/custom/custom.services.yml @@ -7,5 +7,8 @@ services: tags: - { name: event_subscriber } + Drupal\custom\Repository\PostRepository: + autowire: true + Drupal\custom\Service\TalkCounter: autowire: true diff --git a/web/modules/custom/custom/src/Entity/Node/Post.php b/web/modules/custom/custom/src/Entity/Node/Post.php index 30427a35b..4f33f9bf6 100644 --- a/web/modules/custom/custom/src/Entity/Node/Post.php +++ b/web/modules/custom/custom/src/Entity/Node/Post.php @@ -18,6 +18,10 @@ use Drupal\node\Entity\Node; */ class Post extends Node implements ContentEntityBundleInterface { + public function hasBeenSentToSocialMedia(): bool { + return (bool) $this->get('field_sent_to_social_media')->getString(); + } + public function hasTweet(): bool { return (bool) $this->get('field_has_tweet')->getString(); } diff --git a/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php index 89e72fc51..ae1e90845 100644 --- a/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php +++ b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -37,7 +37,10 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { return; } - // TODO: Check that the post has not already been pushed. + // If this post has already been sent to social media, do not send it again. + if ($entity->hasBeenSentToSocialMedia()) { + return; + } $url = \Drupal::configFactory()->get('opdavies_talks.config') ->get('zapier_post_tweet_url'); @@ -47,6 +50,8 @@ final class PushBlogPostToSocialMedia implements EventSubscriberInterface { 'message' => $entity->toTweet(), ], ]); + + $entity->set('field_sent_to_social_media', TRUE); } } diff --git a/web/modules/custom/custom/src/Repository/PostRepository.php b/web/modules/custom/custom/src/Repository/PostRepository.php new file mode 100644 index 000000000..809c9ae92 --- /dev/null +++ b/web/modules/custom/custom/src/Repository/PostRepository.php @@ -0,0 +1,27 @@ +nodeStorage = $entityTypeManager->getStorage('node'); + } + + public function getAll(): Collection { + return new Collection( + $this->nodeStorage->loadByProperties([ + 'type' => 'post', + ]) + ); + } + +}