This commit is contained in:
Oliver Davies 2025-06-12 02:10:08 +01:00
parent 9308ce76a5
commit 51299096c5
5 changed files with 48 additions and 29 deletions

View file

@ -5,7 +5,8 @@ declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\opd_podcast\PodcastEpisode; use Drupal\opd_podcast\Episode;
use Drupal\opd_podcast\Guest;
/** /**
* Implements hook_entity_bundle_info_alter(). * Implements hook_entity_bundle_info_alter().
@ -14,7 +15,11 @@ use Drupal\opd_podcast\PodcastEpisode;
*/ */
function opd_podcast_entity_bundle_info_alter(array &$bundles): void { function opd_podcast_entity_bundle_info_alter(array &$bundles): void {
if (isset($bundles['node'])) { if (isset($bundles['node'])) {
$bundles['node'][PodcastEpisode::NODE_TYPE]['class'] = PodcastEpisode::class; $bundles['node'][Episode::NODE_TYPE]['class'] = Episode::class;
}
if (isset($bundles['taxonomy_term'])) {
$bundles['taxonomy_term'][Guest::TERM_TYPE]['class'] = Guest::class;
} }
} }
@ -23,7 +28,7 @@ function opd_podcast_entity_bundle_info_alter(array &$bundles): void {
* @param array<non-empty-string, mixed> $context * @param array<non-empty-string, mixed> $context
*/ */
function opd_podcast_node_links_alter(array &$links, NodeInterface $entity, array &$context): void { function opd_podcast_node_links_alter(array &$links, NodeInterface $entity, array &$context): void {
if (!$entity instanceof PodcastEpisode) { if (!$entity instanceof Episode) {
return; return;
} }
@ -83,7 +88,7 @@ function opd_podcast_tokens(string $type, array $tokens, array $data, array $opt
switch ($name) { switch ($name) {
case 'guest-names': case 'guest-names':
$node = $data['node'] ?? NULL; $node = $data['node'] ?? NULL;
assert($node instanceof PodcastEpisode); assert($node instanceof Episode);
$replacements[$original] = strval($node->getGuests()); $replacements[$original] = strval($node->getGuests());
break; break;

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::new($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

@ -7,12 +7,12 @@ namespace Drupal\opd_podcast;
use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\TermInterface;
/** /**
* @implements \IteratorAggregate<TermInterface> * @implements \IteratorAggregate<Guest>
*/ */
final class PodcastGuests implements \ArrayAccess, \Countable, \IteratorAggregate, \Stringable { final class Guests implements \ArrayAccess, \Countable, \IteratorAggregate, \Stringable {
/** /**
* @param TermInterface[] $guests * @param Guest[] $guests
*/ */
private function __construct(private array $guests) { private function __construct(private array $guests) {
} }
@ -44,16 +44,16 @@ final class PodcastGuests implements \ArrayAccess, \Countable, \IteratorAggregat
public function __toString(): string { public function __toString(): string {
// TODO: allow for more than two guests. // TODO: allow for more than two guests.
if (count($this->guests) === 2) { if (count($this->guests) === 2) {
assert($this->guests[1] instanceof TermInterface); assert($this->guests[1] instanceof Guest);
return sprintf('%s %s %s', $this->guests[0]->label(), t('and'), $this->guests[1]->label()); return sprintf('%s %s %s', $this->guests[0]->getName(), t('and'), $this->guests[1]->getName());
} }
return strval($this->guests[0]->label()); return strval($this->guests[0]->getName());
} }
/** /**
* @param TermInterface[] $guests * @param Guest[] $guests
*/ */
public static function new(array $guests): self { public static function new(array $guests): self {
return new self($guests); return new self($guests);

View file

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