Rename custom module directories
- Rename `opdavies_blog` to `blog`. - Rename `opdavies_blog_test` to `blog_test`. - Rename `opdavies_talks` to `talks`. - Rename `opdavies_talks_test` to `talks_test`. The files within the directories haven't changed, so there is no breaking change caused by renaming the directories. Please enter the commit message for your changes. Lines starting
This commit is contained in:
parent
d7d5a6c8a3
commit
cbe60209e6
59 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Config\ImmutableConfig;
|
||||
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
||||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
final class PushBlogPostToSocialMedia implements EventSubscriberInterface {
|
||||
|
||||
private ClientInterface $client;
|
||||
|
||||
private ImmutableConfig $config;
|
||||
|
||||
public function __construct(
|
||||
ConfigFactoryInterface $configFactory,
|
||||
Client $client
|
||||
) {
|
||||
$this->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(BaseEntityEvent $event): void {
|
||||
$entity = $event->getEntity();
|
||||
|
||||
if ($entity->getEntityTypeId() != 'node') {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var Post $entity */
|
||||
if ($entity->bundle() != 'post') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$entity->isPublished()) {
|
||||
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('field_sent_to_social_media', TRUE);
|
||||
$entity->save();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog\EventSubscriber;
|
||||
|
||||
use Drupal\hook_event_dispatcher\Event\Entity\BaseEntityEvent;
|
||||
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
final class ReorderBlogTags 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 Post $entity */
|
||||
if ($entity->bundle() != 'post') {
|
||||
return;
|
||||
}
|
||||
|
||||
$sortedTags = $entity->getTags()
|
||||
->sortBy(fn(TermInterface $tag) => $tag->label());
|
||||
|
||||
$entity->setTags($sortedTags->toArray());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue