diff --git a/tools/ansible/vars/deploy_vars.yml b/tools/ansible/vars/deploy_vars.yml index aef8d0633..e619777ab 100644 --- a/tools/ansible/vars/deploy_vars.yml +++ b/tools/ansible/vars/deploy_vars.yml @@ -15,6 +15,8 @@ ansistrano_before_symlink_tasks_file: '{{ playbook_dir }}/deploy/before-symlink. app_hash_salt: '{{ vault_app_hash_salt }}' +zapier_post_tweet_url: '{{ vault_zapier_post_tweet_url }}' + release_drush_path: '{{ ansistrano_release_path.stdout }}/vendor/bin/drush' release_web_path: '{{ ansistrano_release_path.stdout }}/{{ project_web_dir }}' release_theme_path: '{{ release_web_path }}/themes/custom/opdavies' @@ -52,3 +54,4 @@ drupal_settings: $settings['reverse_proxy_header'] = 'CF-Connecting-IP'; $settings['omit_vary_cookie'] = TRUE; + $config['opdavies_talks.config']['zapier_post_tweet_url'] = '{{ zapier_post_tweet_url }}'; diff --git a/tools/ansible/vars/deploy_vault.yml b/tools/ansible/vars/deploy_vault.yml index f1b03a139..d9ac35292 100644 --- a/tools/ansible/vars/deploy_vault.yml +++ b/tools/ansible/vars/deploy_vault.yml @@ -1,8 +1,12 @@ $ANSIBLE_VAULT;1.1;AES256 -39323539326634383533336262633230666630366666363935643462306538366338666436663235 -6231616134366432633965376535303635396134333661640a393961633431383165653439343436 -39303333346162386436393133303633636565323730643732396431623464623631396138343434 -3166346130643937340a343433393831396638643434653933343033353430326633376333396462 -66363461363435373462643037353661346435383336336137613837633933393931613833313037 -33393064383939666335323733346164643036366232656362326461373031303365386266663133 -343633356331333831633730396562616634 +62643334643263653766663535353366623863386531373963316461633838393065303837353534 +3839316435356438653861303830373831343830373738620a306166646262343161616631656632 +63633838363735663165373133326438626465653064306562636232363662393363376539656632 +6535616630396334660a376664353566393862663239653963326531663332636364626139356639 +34623762666236303264623439363066383366623362323236313832653532376236366439366233 +37653234626463386435656438366436303533643531373032333333303964623636316361396437 +63383737633637633034396434323137303031346261656132356461303763326538363763643531 +34313130386636316332626536643461313532363833373261383131616461383639613934383463 +66626333633139323430636333636564383863363633663235373736383332613162623664643234 +35313831326539333762386635393232653133383538316466356434636635643130386435393139 +386561333039316230363637323935363135 diff --git a/web/modules/custom/custom/config/schema/custom.schema.yml b/web/modules/custom/custom/config/schema/custom.schema.yml new file mode 100644 index 000000000..e7fbf81f0 --- /dev/null +++ b/web/modules/custom/custom/config/schema/custom.schema.yml @@ -0,0 +1,6 @@ +opdavies_talks.settings: + type: config_object + label: 'Talks module configuration' + mapping: + zapier_post_tweet_url: + type: string diff --git a/web/modules/custom/custom/custom.services.yml b/web/modules/custom/custom/custom.services.yml index 1993b99c1..c553e28a4 100644 --- a/web/modules/custom/custom/custom.services.yml +++ b/web/modules/custom/custom/custom.services.yml @@ -3,5 +3,9 @@ services: tags: - { name: event_subscriber } + Drupal\custom\EventSubscriber\PushBlogPostToSocialMedia: + tags: + - { name: event_subscriber } + 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 c41e90805..30427a35b 100644 --- a/web/modules/custom/custom/src/Entity/Node/Post.php +++ b/web/modules/custom/custom/src/Entity/Node/Post.php @@ -22,4 +22,12 @@ class Post extends Node implements ContentEntityBundleInterface { return (bool) $this->get('field_has_tweet')->getString(); } + public function toTweet(): string { + // TODO: Add tags. + + $parts = [$this->label(), $this->url('canonical', ['absolute' => TRUE])]; + + return implode(PHP_EOL . PHP_EOL, $parts); + } + } diff --git a/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php new file mode 100644 index 000000000..89e72fc51 --- /dev/null +++ b/web/modules/custom/custom/src/EventSubscriber/PushBlogPostToSocialMedia.php @@ -0,0 +1,52 @@ + 'onEntityPreSave', + ]; + } + + public function onEntityPresave(BaseEntityEvent $event): void { + $entity = $event->getEntity(); + + if ($entity->getEntityTypeId() != 'node') { + return; + } + + /** @var NodeInterface $entity */ + if ($entity->bundle() != 'post') { + return; + } + + if (!$entity->isPublished()) { + return; + } + + // TODO: Check that the post has not already been pushed. + + $url = \Drupal::configFactory()->get('opdavies_talks.config') + ->get('zapier_post_tweet_url'); + + \Drupal::httpClient()->post($url, [ + 'form_params' => [ + 'message' => $entity->toTweet(), + ], + ]); + } + +}