Move all files to old/drupal/

This commit is contained in:
Oliver Davies 2025-10-02 07:57:25 +01:00
parent 8203e983d5
commit 7d76bf2968
468 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,101 @@
<?php
namespace Drupal\opdavies_blog\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\opdavies_blog\Repository\RelatedPostsRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Illuminate\Support\Collection;
/**
* @Block(
* id = "opdavies_blog_related_posts",
* admin_label = @Translation("Related Posts"),
* category = @Translation("Blog")
* )
*/
class RelatedPostsBlock extends BlockBase implements ContainerFactoryPluginInterface {
private CurrentRouteMatch $currentRouteMatch;
private RelatedPostsRepository $relatedPostsRepository;
public function __construct(
array $configuration,
string $pluginId,
array $pluginDefinition,
CurrentRouteMatch $currentRouteMatch,
RelatedPostsRepository $relatedPostsRepository
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->currentRouteMatch = $currentRouteMatch;
$this->relatedPostsRepository = $relatedPostsRepository;
}
public static function create(
ContainerInterface $container,
array $configuration,
$pluginId,
$pluginDefinition
): self {
return new self(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('current_route_match'),
$container->get(RelatedPostsRepository::class)
);
}
public function build(): array {
$currentPost = $this->currentRouteMatch->getParameter('node');
/** @var Collection|Post[] $relatedPosts */
$relatedPosts = $this->relatedPostsRepository->getFor($currentPost);
if ($relatedPosts->isEmpty()) {
return [];
}
$build['content'] = [
'#items' => $relatedPosts
->sortByDesc(fn(Post $post) => $post->getNode()->getCreatedTime())
->map(fn(Post $post) => $this->generateLinkToPost($post))
->slice(0, 3)
->toArray(),
'#theme' => 'item_list',
];
return $build;
}
public function getCacheMaxAge(): int {
return 604800;
}
public function getCacheContexts(): array {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}
public function getCacheTags(): array {
/** @var Post $post */
$post = $this->currentRouteMatch->getParameter('node');
return Cache::mergeTags(parent::getCacheTags(), ["node:{$post->id()}"]);
}
private function generateLinkToPost(Post $post): Link {
return Link::createFromRoute(
$post->label(),
'entity.node.canonical',
['node' => $post->id()]
);
}
}

View file

@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_blog\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\opdavies_blog\Entity\Node\Post;
use Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher;
use Drupal\opdavies_blog\Service\PostPusher\IntegromatPostPusher;
use Drupal\opdavies_blog\Service\PostPusher\PostPusher;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @QueueWorker(
* id = "opdavies_blog.push_post_to_social_media",
* title = "Push a blog post to social media",
* cron = {"time": 30}
* )
*/
final class PostPusherQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
private EntityStorageInterface $nodeStorage;
/**
* @var array|PostPusher[]
*/
private array $postPushers;
public function __construct(
array $configuration,
string $pluginId,
array $pluginDefinition,
EntityStorageInterface $nodeStorage,
array $postPushers
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->nodeStorage = $nodeStorage;
$this->postPushers = $postPushers;
}
public static function create(
ContainerInterface $container,
array $configuration,
$pluginId,
$pluginDefinition
) {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get('entity_type.manager')->getStorage('node'),
[
$container->get(IftttPostPusher::class),
$container->get(IntegromatPostPusher::class),
]
);
}
public function processItem($data): void {
/** @var Post $post */
['post' => $post] = $data;
if (!$this->shouldBePushed($post)) {
return;
}
if (!$post->getNode()->isLatestRevision()) {
$node = $this->nodeStorage->load($post->id());
$post = Post::createFromNode($node);
if (!$this->shouldBePushed($post)) {
return;
}
}
foreach ($this->postPushers as $pusher) {
$pusher->push($post);
}
$post->set(Post::FIELD_SENT_TO_SOCIAL_MEDIA, TRUE);
$post->save();
}
private function shouldBePushed(Post $post): bool {
if ($post->isExternalPost()) {
return FALSE;
}
if (!$post->getNode()->isPublished()) {
return FALSE;
}
if (!$post->shouldSendToSocialMedia()) {
return FALSE;
}
if ($post->hasBeenSentToSocialMedia()) {
return FALSE;
}
return TRUE;
}
}