Automatically create tweets for new posts

This commit is contained in:
Oliver Davies 2020-07-19 20:12:08 +01:00
parent eafcb103b8
commit f488b5c023
6 changed files with 84 additions and 7 deletions

View file

@ -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 }}';

View file

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

View file

@ -0,0 +1,6 @@
opdavies_talks.settings:
type: config_object
label: 'Talks module configuration'
mapping:
zapier_post_tweet_url:
type: string

View file

@ -3,5 +3,9 @@ services:
tags:
- { name: event_subscriber }
Drupal\custom\EventSubscriber\PushBlogPostToSocialMedia:
tags:
- { name: event_subscriber }
Drupal\custom\Service\TalkCounter:
autowire: true

View file

@ -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);
}
}

View file

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Drupal\custom\EventSubscriber;
use Drupal\custom\Entity\Node\Post;
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
/**
* @inheritDoc
*/
public static function getSubscribedEvents() {
return [
HookEventDispatcherInterface::ENTITY_PRE_SAVE => '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(),
],
]);
}
}