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,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,
]);
}
}