Move all files to tome/

This commit is contained in:
Oliver Davies 2025-10-01 00:05:52 +01:00
parent 5675bcfc36
commit 674daab35b
2874 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,4 @@
name: Daily Emails
type: module
core_version_requirement: ^11
package: oliverdavies.uk

View file

@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\opd_daily_emails\Action\AddRandomCtaToDailyEmail;
use Drupal\opd_daily_emails\DailyEmail;
use Drupal\opd_daily_emails\DailyEmailNodeRepository;
/**
* Implements hook_entity_bundle_info_alter().
*
* @param array<non-empty-string, array{class: non-empty-string}> $bundles
*/
function opd_daily_emails_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) {
$bundles['node'][DailyEmail::NODE_TYPE]['class'] = DailyEmail::class;
}
}
/**
* Implements hook_entity_presave().
*/
function opd_daily_emails_entity_presave(Drupal\Core\Entity\EntityInterface $entity): void {
if (!$entity instanceof DailyEmail) {
return;
}
\Drupal::service(AddRandomCtaToDailyEmail::class)($entity);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* @param array{'#action': string, '#attributes': array<non-empty-string, mixed>} $form
*/
function opd_daily_emails_form_opd_daily_emails_kit_subscription_form_alter(array &$form): void {
$form['#action'] = 'https://app.convertkit.com/forms/3546728/subscriptions';
$form['#attributes']['data-format'] = 'inline';
$form['#attributes']['data-sv-form'] = '3546728';
$form['#attributes']['data-uid'] = 'f0c1d2b57f';
$form['#attributes']['data-version'] = 5;
}
/**
* Implements hook_token_info().
*
* @return array{tokens: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}[]}, types: array{opd-daily-emails: array{description: TranslatableMarkup, name: TranslatableMarkup}}}
*/
function opd_daily_emails_token_info(): array {
$tokens = [];
$type = [
'description' => t('Tokens related to daily emails.'),
'name' => t('Daily emails'),
];
$tokens['email-count'] = [
'description' => t('The number of sent daily emails.'),
'name' => t('Daily email count'),
];
return [
'tokens' => [
'opd-daily-emails' => $tokens,
],
'types' => [
'opd-daily-emails' => $type,
],
];
}
/**
* Implements hook_tokens().
*
* @param array<non-empty-string, non-empty-string> $tokens
* @param array<non-empty-string, mixed> $data
* @param array<non-empty-string, mixed> $options
*
* @return array<non-empty-string, mixed>
*/
function opd_daily_emails_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleableMetadata) : array {
$replacements = [];
if ($type === 'opd-daily-emails') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'email-count':
$dailyEmailRepository = \Drupal::service(DailyEmailNodeRepository::class);
$dailyEmails = $dailyEmailRepository->getAll();
$replacements[$original] = $dailyEmails->count();
break;
}
}
}
return $replacements;
}

View file

@ -0,0 +1,5 @@
services:
Drupal\opd_daily_emails\Action\AddRandomCtaToDailyEmail:
autowire: true
Drupal\opd_daily_emails\DailyEmailNodeRepository:
autowire: true

View file

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Action;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\opd_daily_emails\Ctas;
use Drupal\opd_daily_emails\DailyEmail;
readonly final class AddRandomCtaToDailyEmail {
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
}
public function __invoke(DailyEmail $email): void {
if (!$this->shouldUpdate($email)) {
return;
}
$ctas = $this->getCtas();
if ($ctas->isEmpty()) {
return;
}
$email->set('field_daily_email_cta', $ctas->getRandomCta());
}
private function getCtas(): Ctas {
$nodeStorage = $this->entityTypeManager->getStorage('node');
$query = $nodeStorage->getQuery();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', 'daily_email_cta');
$query->accessCheck();
$ctaNodes = $nodeStorage->loadMultiple($query->execute());
return Ctas::fromNodes($ctaNodes);
}
private function shouldUpdate(DailyEmail $email): bool {
if (!$email->isNew()) {
return FALSE;
}
if ($email->get('field_daily_email_cta')->getValue() !== []) {
return FALSE;
}
// TODO: only check this if the body has a value, because it could be empty.
$body = $email->get('body')->value;
assert(is_string($body));
if (str_contains(haystack: $body, needle: 'P.S.')) {
return FALSE;
}
return TRUE;
}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\node\NodeInterface;
use Webmozart\Assert\Assert;
final class Ctas {
public function isEmpty(): bool {
return empty($this->ctas);
}
public function getRandomCta(): NodeInterface {
return $this->ctas[array_rand($this->ctas)];
}
/**
* @param list<NodeInterface> $nodes
*/
public static function fromNodes(array $nodes): self {
return new self($nodes);
}
/**
* @param list<NodeInterface> $ctas
*/
private function __construct(
private array $ctas,
) {
Assert::allIsInstanceOf($ctas, NodeInterface::class);
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
final class DailyEmail extends Node implements NodeInterface {
public const NODE_TYPE = 'daily_email';
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
final class DailyEmailNodeRepository implements DailyEmailRepositoryInterface {
public function __construct(
public readonly EntityTypeManagerInterface $entityTypeManager,
) {
}
public function getAll(): DailyEmails {
$nodeStorage = $this->entityTypeManager
->getStorage('node');
$query = $nodeStorage->getQuery();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', 'daily_email');
$query->accessCheck(TRUE);
$nodeIds = $query->execute();
/** @var DailyEmail[] */
$nodes = $nodeStorage->loadMultiple($nodeIds);
return DailyEmails::fromEmails($nodes);
}
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
interface DailyEmailRepositoryInterface {
public function getAll(): DailyEmails;
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails;
use Drupal\node\NodeInterface;
use Webmozart\Assert\Assert;
final class DailyEmails implements \Countable {
public function count(): int {
return count($this->emails);
}
public function first(): NodeInterface {
return array_values($this->emails)[0];
}
/**
* @param DailyEmail[] $emails
*/
public static function fromEmails(array $emails): self {
return new self($emails);
}
/**
* @param array<positive-int, DailyEmail> $emails
*/
private function __construct(
private array $emails,
) {
Assert::allIsInstanceOf($emails, NodeInterface::class);
}
}

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
final class KitSubscriptionForm extends FormBase {
public function getFormId(): string {
return 'opd_daily_emails_kit_subscription_form';
}
/**
* @param array<non-empty-string, array{}> $form
* @param FormStateInterface $formState
*
* @return array<non-empty-string, mixed>
*/
public function buildForm(array $form, FormStateInterface $formState): array {
// TODO: refactor to a custom theme function.
// TODO: make this optional. I may not want the intro text on every form - especially when two are on the same page.
$form['intro'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => Settings::get('prose_classes'),
'style' => 'margin-bottom: 1rem;',
],
];
$form['intro']['text']['#markup'] = '<p>' . t('Subscribe to my daily newsletter for software professionals on software development and delivery, Drupal, DevOps, community, and open-source.') . '</p>';
$form['email_address'] = [
'#placeholder' => 'me@example.com',
'#title' => $this->t('What is your best email address?'),
'#type' => 'email',
];
$form['actions']['submit'] = [
'#attributes' => [
'class' => ['inline-flex justify-center items-center py-3 px-6 w-full font-medium text-white no-underline rounded-md border duration-200 ease-in-out hover:bg-white focus:bg-white border-blue-primary bg-blue-primary transition-color hover:text-blue-primary focus:text-blue-primary'],
'data-element' => 'submit',
],
'#type' => 'button',
'#value' => $this->t('Get daily emails') . ' →',
];
return $form;
}
/**
* @param array<non-empty-string, mixed> $form
* @param FormStateInterface $formState
*/
public function submitForm(array &$form, FormStateInterface $formState): void {
}
}

View file

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_daily_emails\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\opd_daily_emails\Form\KitSubscriptionForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
#[Block(
admin_label: new TranslatableMarkup("Kit Subscription Block"),
category: new TranslatableMarkup("Daily emails"),
id: "opd_daily_emails_kit_subscription_block",
)]
final class KitSubscriptionFormBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* @param array<string, mixed> $configuration
* @param string $pluginId
* @param array<string, mixed> $pluginDefinition
* @param FormBuilderInterface $formBuilder
*/
public function __construct(
array $configuration,
string $pluginId,
array $pluginDefinition,
private FormBuilderInterface $formBuilder,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
}
/**
* @return array<non-empty-string, mixed>
*/
public function build(): array {
return $this->formBuilder->getForm(KitSubscriptionForm::class);
}
/**
* @param ContainerInterface $container
* @param array<string, mixed> $configuration
* @param string $pluginId
* @param array<string, mixed> $pluginDefinition
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition): self {
return new static(
$configuration,
$pluginId,
$pluginDefinition,
$container->get(FormBuilderInterface::class),
);
}
}

View file

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_daily_emails\Functional;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class CallToActionTest extends ExistingSiteBase {
public function test_saving_an_email_node_without_a_cta_will_populate_one(): void {
$cta = $this->createNode([
'type' => 'daily_email_cta',
]);
$email = $this->createNode([
'field_daily_email_cta' => NULL,
'type' => 'daily_email',
]);
$this->assertNotEmpty($email->get('field_daily_email_cta')->getValue());
// TODO: assert the returned text.
}
public function test_saving_an_email_node_with_a_cta_will_keep_the_same_cta(): void {
$cta = $this->createNode([
'type' => 'daily_email_cta',
]);
$email = $this->createNode([
'field_daily_email_cta' => $cta,
'type' => 'daily_email',
]);
$email->set('title', 'Updated');
$email->save();
$value = $email->get('field_daily_email_cta')->getValue();
assert(is_array($value));
assert(isset($value[0]['target_id']));
$this->assertSame(
actual: $value[0]['target_id'],
expected: $cta->id(),
);
}
public function test_cta_in_body_prevents_automatically_adding_a_cta(): void {
$email = $this->createNode([
'body' => [
'format' => 'basic_html',
'value' => 'P.S. This email already contains a CTA.',
],
'field_daily_email_cta' => NULL,
'type' => 'daily_email',
]);
$this->assertEmpty($email->get('field_daily_email_cta'));
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Drupal\Tests\opd_daily_emails\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\opd_daily_emails\Traits\DailyEmailTestTrait;
use Drupal\Tests\token\Functional\TokenTestTrait;
class DailyEmailTokenTest extends BrowserTestBase {
use DailyEmailTestTrait;
use RandomGeneratorTrait;
use TokenTestTrait;
public $defaultTheme = 'stark';
protected static $modules = [
'node',
'opd_daily_emails',
];
public function test_the_token_returns_the_number_of_published_daily_emails(): void {
$this->createDailyEmailNode(
isPublished: TRUE,
title: 'a',
);
$this->createDailyEmailNode(
isPublished: FALSE,
title: 'b',
);
$this->createDailyEmailNode(
isPublished: TRUE,
title: 'c',
);
$this->assertToken(
data: [],
expected: 2,
token: 'email-count',
type: 'opd-daily-emails',
);
}
}

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_daily_emails\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\opd_daily_emails\Traits\DailyEmailTestTrait;
use Drupal\opd_daily_emails\DailyEmailNodeRepository;
use Drupal\opd_daily_emails\DailyEmailRepositoryInterface;
final class DailyEmailNodeRepositoryTest extends EntityKernelTestBase {
use DailyEmailTestTrait;
protected static $modules = [
'node',
'opd_daily_emails',
];
public function test_get_all_published_daily_emails(): void {
$this->createDailyEmailNode(
isPublished: TRUE,
title: 'Published',
);
$this->createDailyEmailNode(
isPublished: FALSE,
title: 'Unpublished',
);
$repository = $this->container->get(DailyEmailNodeRepository::class);
assert($repository instanceof DailyEmailRepositoryInterface);
$emails = $repository->getAll();
$this->assertCount(
expectedCount: 1,
haystack: $emails,
);
$this->assertSame(
actual: $emails->first()->label(),
expected: 'Published',
);
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_daily_emails\Traits;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\node\NodeInterface;
trait DailyEmailTestTrait {
use NodeCreationTrait;
/**
* @param non-empty-string $title
* @param bool $isPublished
*/
protected function createDailyEmailNode(
string $title,
bool $isPublished,
): NodeInterface {
return $this->createNode([
'status' => $isPublished,
'title' => $title,
'type' => 'daily_email',
]);
}
}

View file

@ -0,0 +1,5 @@
name: Podcast
description: Custom functionality for podcasts.
core_version_requirement: ^11
type: module
package: Custom

View file

@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;
use Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber;
use Drupal\opd_podcast\Episode;
use Drupal\opd_podcast\Guest;
/**
* Implements hook_form_FORM_ID_alter().
*
* @param array{} $form
*/
function opd_podcast_form_node_podcast_episode_form_alter(array &$form, FormStateInterface $formState): void {
$nextEpisodeNumber = \Drupal::service(GetNextPodcastEpisodeNumber::class);
assert($nextEpisodeNumber instanceof GetNextPodcastEpisodeNumber);
$form['field_episode_number']['widget'][0]['value']['#default_value'] = $nextEpisodeNumber();
}
/**
* Implements hook_entity_bundle_info_alter().
*
* @param array<non-empty-string, array{class: non-empty-string}> $bundles
*/
function opd_podcast_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) {
$bundles['node'][Episode::NODE_TYPE]['class'] = Episode::class;
}
if (isset($bundles['taxonomy_term'])) {
$bundles['taxonomy_term'][Guest::TERM_TYPE]['class'] = Guest::class;
}
}
/**
* @param array<non-empty-string, array<non-empty-string, array{}>> $links
* @param array<non-empty-string, mixed> $context
*/
function opd_podcast_node_links_alter(array &$links, NodeInterface $entity, array &$context): void {
if (!$entity instanceof Episode) {
return;
}
$links['node']['#links']['node-readmore']['title'] = t('Listen now<span class="visually-hidden"> to @title</span> →');
$links['node']['#links']['node-readmore']['attributes']['class'] = [
'p-0',
];
$links['#attributes']['class'][] = 'list-none';
$links['#attributes']['class'][] = 'm-0';
$links['#attributes']['class'][] = 'p-0';
}
/**
* Implements hook_token_info().
*
* @return array{tokens: array{opd-podcast: array{description: TranslatableMarkup, name: TranslatableMarkup}[]}, types: array{opd-podcast: array{description: TranslatableMarkup, name: TranslatableMarkup}}}
*/
function opd_podcast_token_info(): array {
$tokens = [];
$type = [
'description' => t('Tokens related to podcasts.'),
'name' => t('Podcasts'),
];
$tokens['guest-names'] = [
'description' => t('The names of the guests on a podcast episode.'),
'name' => t('Guest names'),
];
return [
'tokens' => [
'opd-podcast' => $tokens,
],
'types' => [
'opd-podcast' => $type,
],
];
}
/**
* Implements hook_tokens().
*
* @param array<non-empty-string, non-empty-string> $tokens
* @param array<non-empty-string, mixed> $data
* @param array<non-empty-string, mixed> $options
*
* @return array<non-empty-string, mixed>
*/
function opd_podcast_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleableMetadata) : array {
$replacements = [];
if ($type === 'opd-podcast') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'guest-names':
$node = $data['node'] ?? NULL;
assert($node instanceof Episode);
$replacements[$original] = strval($node->getGuests());
break;
}
}
}
return $replacements;
}

View file

@ -0,0 +1,5 @@
services:
Drupal\opd_podcast\Action\GetNextPodcastEpisodeNumber:
autowire: true
Drupal\opd_podcast\Repository\PodcastNodeRepository:
autowire: true

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast\Action;
use Drupal\opd_podcast\Repository\PodcastNodeRepository;
readonly final class GetNextPodcastEpisodeNumber {
public function __construct(private PodcastNodeRepository $repository) {
}
public function __invoke(): int {
$episodes = $this->repository->getPublished();
return count($episodes) + 1;
}
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
final class Episode extends Node implements NodeInterface {
public const NODE_TYPE = 'podcast_episode';
public function getGuests(): Guests {
return Guests::fromGuests($this->get('field_podcast_guests')->referencedEntities());
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
final class Guest extends Term implements TermInterface {
public const TERM_TYPE = 'podcast_guest';
}

View file

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast;
use Webmozart\Assert\Assert;
/**
* @implements \IteratorAggregate<Guest>
*/
final class Guests implements \Countable, \IteratorAggregate, \Stringable {
public function __toString(): string {
// TODO: allow for more than two guests.
if ($this->count() === 2) {
assert($this->first() instanceof Guest);
return sprintf('%s %s %s', $this->first()->getName(), t('and'), $this->get(1)->getName());
}
return strval($this->first()?->getName());
}
public function count(): int {
return count($this->guests);
}
public function first(): ?Guest {
return array_values($this->guests)[0];
}
public function getIterator(): \Traversable {
return new \ArrayIterator($this->guests);
}
/**
* @param Guest[] $guests
*/
public static function fromGuests(array $guests): self {
return new self($guests);
}
/**
* @param Guest[] $guests
*/
private function __construct(private array $guests) {
Assert::allIsInstanceOf($guests, Guest::class);
}
private function get(int $offset): Guest {
return $this->guests[$offset];
}
}

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\node\NodeStorageInterface;
use Drupal\opd_podcast\Episode;
readonly final class PodcastNodeRepository {
private NodeStorageInterface $nodeStorage;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->nodeStorage = $entityTypeManager->getStorage('node');
}
/**
* @return list<NodeInterface>
*/
public function getPublished(): array {
$query = $this->nodeStorage->getQuery();
$query->accessCheck();
$query->condition('status', NodeInterface::PUBLISHED);
$query->condition('type', Episode::NODE_TYPE);
$nodeIds = $query->execute();
return $this->nodeStorage->loadMultiple($nodeIds);
}
}

View file

@ -0,0 +1,28 @@
langcode: en
status: true
dependencies:
config:
- field.storage.node.field_podcast_guests
- node.type.podcast_episode
- taxonomy.vocabulary.podcast_guest
id: node.podcast_episode.field_podcast_guests
field_name: field_podcast_guests
entity_type: node
bundle: podcast_episode
label: Guests
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:taxonomy_term'
handler_settings:
target_bundles:
podcast_guest: podcast_guest
sort:
field: name
direction: asc
auto_create: true
auto_create_bundle: ''
field_type: entity_reference

View file

@ -0,0 +1,19 @@
langcode: en
status: true
dependencies:
module:
- node
- taxonomy
id: node.field_podcast_guests
field_name: field_podcast_guests
entity_type: node
type: entity_reference
settings:
target_type: taxonomy_term
module: core
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false

View file

@ -0,0 +1,11 @@
langcode: en
status: true
dependencies: { }
third_party_settings: { }
name: 'Podcast episode'
type: podcast_episode
description: null
help: null
new_revision: true
preview_mode: 1
display_submitted: true

View file

@ -0,0 +1,8 @@
langcode: en
status: true
dependencies: { }
name: 'Podcast guest'
vid: podcast_guest
description: null
weight: 0
new_revision: false

View file

@ -0,0 +1,8 @@
name: Podcast Test
description: A test module for podcasts.
core_version_requirement: ^11
type: module
package: oliverdavies.uk
hidden: true
dependencies:
- drupal:node

View file

@ -0,0 +1,79 @@
<?php
namespace Drupal\Tests\opd_podcast\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\token\Functional\TokenTestTrait;
use Drupal\node\NodeInterface;
use Drupal\opd_podcast\Episode;
use Drupal\opd_podcast\Guest;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
class PodcastTokenTest extends BrowserTestBase {
use NodeCreationTrait;
use TokenTestTrait;
public $defaultTheme = 'stark';
protected static $modules = [
'opd_podcast',
'opd_podcast_test',
'taxonomy',
];
/**
* @param non-empty-string[] $guestNames
* @param non-empty-string $expected
*
* @dataProvider podcastNodeProvider
*/
public function test_guest_name_token(array $guestNames, string $expected): void {
$node = $this->createPodcastNode(
guestNames: $guestNames,
);
$this->assertToken(
data: ['node' => $node],
expected: $expected,
token: 'guest-names',
type: 'opd-podcast',
);
}
public function podcastNodeProvider(): \Generator {
return [
yield 'Single guest' => [['Matt Glaman'], 'Matt Glaman'],
yield 'Two guests' => [['Emma Horrell', 'Luke McCormick'], 'Emma Horrell and Luke McCormick'],
];
}
/**
* @param array<int, non-empty-string> $guestNames
*
* @return list<TermInterface>
*/
private function createGuestTerms(array $guestNames): array {
return array_map(
array: $guestNames,
callback: fn (string $guestName): TermInterface => Term::create([
'name' => $guestName,
'vid' => Guest::TERM_TYPE,
]),
);
}
/**
* @param non-empty-string[] $guestNames
*/
private function createPodcastNode(array $guestNames): NodeInterface {
return $this->createNode([
'field_podcast_guests' => $this->createGuestTerms($guestNames),
'title' => '::title::',
'type' => Episode::NODE_TYPE,
]);
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace Drupal\Tests\opd_podcast\Functional;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\node\NodeInterface;
use Drupal\opd_podcast\Episode;
use Drupal\opd_podcast\Repository\PodcastNodeRepository;
class PodcastNodeRepositoryTest extends EntityKernelTestBase {
use NodeCreationTrait;
protected static $modules = [
'node',
'opd_podcast',
'taxonomy',
];
public function test_it_returns_published_podcast_episodes(): void {
$this->createEpisode([
'title' => 'Episode A',
'status' => NodeInterface::PUBLISHED,
]);
$this->createEpisode([
'title' => 'Episode B',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$this->createEpisode([
'title' => 'Episode C',
'status' => NodeInterface::PUBLISHED,
]);
$this->createEpisode([
'title' => 'Episode D',
'status' => NodeInterface::PUBLISHED,
]);
$repository = $this->container->get(PodcastNodeRepository::class);
$episodes = $repository->getPublished();
$this->assertCount(expectedCount: 3, haystack: $episodes);
$titles = array_map(
array: $episodes,
callback: fn (NodeInterface $episode): string => (string) $episode->getTitle(),
);
$this->assertSame(
actual: array_values($titles),
expected: ['Episode A', 'Episode C', 'Episode D'],
);
}
/**
* @param array<non-empty-string, mixed> $values
*/
private function createEpisode(array $values): NodeInterface {
return $this->createNode([
'type' => Episode::NODE_TYPE,
...$values,
]);
}
}

View file

@ -0,0 +1,5 @@
name: Presentations
description: Custom functionality for presentations.
core_version_requirement: ^11
type: module
package: Custom

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
use Drupal\opd_presentations\Event;
use Drupal\opd_presentations\Presentation;
/**
* Implements hook_entity_bundle_info_alter().
*
* @param array<non-empty-string, array{class: non-empty-string}> $bundles
*/
function opd_presentations_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) {
$bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class;
}
if (isset($bundles['paragraph'])) {
$bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class;
}
}

View file

@ -0,0 +1,6 @@
services:
Drupal\opd_presentations\Action\CountGivenPresentations:
autowire: true
Drupal\opd_presentations\Repository\PresentationNodeRepository:
autowire: true
Drupal\opd_presentations\Repository\PresentationRepositoryInterface: '@Drupal\opd_presentations\Repository\PresentationNodeRepository'

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Action;
use Drupal\opd_presentations\Presentation;
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
readonly final class CountGivenPresentations {
public function __construct(
private PresentationRepositoryInterface $presentationRepository,
) {
}
public function __invoke(): int {
$presentations = $this->presentationRepository->getPublished();
return array_reduce(
array: $presentations,
callback: fn (int $count, Presentation $presentation)
=> $count + $presentation->getEvents()->getPast()->count(),
initial: 0,
);
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
readonly final class Date {
public function toTimestamp(): int {
return $this->date->getTimestamp();
}
public static function fromString(string $date): self {
return new self(new \DateTimeImmutable($date));
}
private function __construct(private \DateTimeImmutable $date) {
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
final class Event extends Paragraph implements ParagraphInterface {
public const PARAGRAPH_TYPE = 'event';
public function getEventDate(): string {
/** @var non-empty-string */
return $this->get('field_date')->value;
}
public function getEventName(): string {
/** @var non-empty-string */
return $this->get('field_event_name')->value;
}
public function isPast(): bool {
return $this->getEventDate() < strtotime('today');
}
}

View file

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
use Webmozart\Assert\Assert;
/**
* @implements \IteratorAggregate<Event>
*/
readonly final class Events implements \Countable, \IteratorAggregate {
public function count(): int {
return count($this->events);
}
public function filter(\Closure $callback): self {
return new self(array_filter(
array: $this->events,
callback: $callback,
));
}
public function first(): Event {
return array_values($this->events)[0];
}
public function getIterator(): \Traversable {
return new \ArrayIterator($this->events);
}
public function getPast(): self {
return (new self($this->events))
->filter(fn (Event $event): bool => $event->isPast());
}
public static function fromDateStrings(string ...$dates): self {
$events = array_map(
array: $dates,
callback: fn (string $date): Event => Event::create([
'field_date' => strtotime($date),
]),
);
return new self($events);
}
/**
* @return Event[]
*/
public function toEvents(): array {
return $this->events;
}
/**
* @param Event[] $events
*/
public static function fromEvents(array $events): self {
return new self($events);
}
/**
* @param Event[] $events
*/
private function __construct(private array $events) {
Assert::allIsInstanceOf($events, Event::class);
}
}

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
final class Presentation extends Node implements NodeInterface {
public const NODE_TYPE = 'presentation';
public function getEvents(): Events {
return Events::fromEvents($this->get('field_events')->referencedEntities());
}
}

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\node\NodeInterface;
use Drupal\node\NodeStorageInterface;
use Drupal\opd_presentations\Presentation;
final class PresentationNodeRepository implements PresentationRepositoryInterface {
private NodeStorageInterface $nodeStorage;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->nodeStorage = $entityTypeManager->getStorage('node');
}
public function getPublished(): array {
$query = $this->query();
$query->condition('status', NodeInterface::PUBLISHED);
$nodeIds = $query->execute();
assert(is_array($nodeIds));
/** @var Presentation[] */
return $this->nodeStorage->loadMultiple($nodeIds);
}
private function query(): QueryInterface {
$query = $this->nodeStorage->getQuery();
$query->accessCheck();
$query->condition('type', Presentation::NODE_TYPE);
return $query;
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Repository;
use Drupal\opd_presentations\Presentation;
interface PresentationRepositoryInterface {
/**
* @return Presentation[]
*/
public function getPublished(): array;
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_presentations\Action;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Action\CountGivenPresentations;
use Drupal\opd_presentations\Events;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class CountGivenPresentationsTest extends ExistingSiteBase {
use PresentationCreationTrait;
public function test_it_counts_events(): void {
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
$this->createPresentation(
Events::fromDateStrings('yesterday'),
);
$this->assertGreaterThanOrEqual(
actual: $action(),
expected: 1,
);
}
public function test_it_only_counts_published_events(): void {
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
// Get the existing presentation count (including existing nodes).
$originalCount = $action();
$this->createPresentation(
events: Events::fromDateStrings('yesterday'),
isPublished: FALSE,
);
// Ensure the count has only increased by one, even though an unpublished
// presentation was created.
$this->assertSame(
actual: $action(),
expected: $originalCount,
);
}
public function test_it_only_counts_past_events(): void {
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
// Get the existing presentation count (including existing nodes).
$originalCount = $action();
$this->assertGreaterThanOrEqual(
actual: $originalCount,
expected: 0,
);
$this->createPresentation(
Events::fromDateStrings('tomorrow', 'yesterday'),
);
// Ensure the count has only increased by one, even though a future and past event were created.
$this->assertSame(
actual: $action(),
expected: $originalCount + 1,
);
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_presentations\Functional;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Events;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationTest extends ExistingSiteBase {
use PresentationCreationTrait;
public function test_only_past_events_are_returned(): void {
$presentation = $this->createPresentation(
events: Events::fromDateStrings('now', 'yesterday', 'tomorrow'),
);
$events = $presentation->getEvents()->getPast();
$this->assertCount(
expectedCount: 1,
haystack: $events,
);
}
}

View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_presentations\Traits;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\ctools\Testing\EntityCreationTrait;
use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\Event;
use Drupal\opd_presentations\Events;
use Drupal\opd_presentations\Presentation;
trait PresentationCreationTrait {
use EntityCreationTrait;
use NodeCreationTrait;
private function createPresentation(Events $events, bool $isPublished = TRUE): Presentation {
$presentation = $this->createNode([
'field_events' => $events->toEvents(),
'status' => $isPublished,
'type' => Presentation::NODE_TYPE,
]);
assert($presentation instanceof Presentation);
return $presentation;
}
private function createEvent(string $eventName, Date $eventDate): Event {
$event = $this->createEntity(
entity_type: 'paragraph',
values: [
'field_date' => $eventDate->toTimestamp(),
'field_event_name' => $eventName,
'type' => Event::PARAGRAPH_TYPE,
],
);
assert($event instanceof Event);
return $event;
}
}