Add a PodcastEpisode bundle class

This commit is contained in:
Oliver Davies 2025-06-12 02:10:08 +01:00
parent 0871649c05
commit 52c1b33711
2 changed files with 33 additions and 4 deletions

View file

@ -5,14 +5,26 @@ declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\NodeInterface;
use Drupal\opd_podcast\Entity\PodcastEpisode;
use Drupal\taxonomy\TermInterface;
/**
* 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'][PodcastEpisode::NODE_TYPE]['class'] = PodcastEpisode::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->bundle() !== 'podcast_episode') {
if (!$entity instanceof PodcastEpisode) {
return;
}
@ -72,10 +84,9 @@ function opd_podcast_tokens(string $type, array $tokens, array $data, array $opt
switch ($name) {
case 'guest-names':
$node = $data['node'] ?? NULL;
assert($node instanceof NodeInterface);
assert($node->hasField('field_podcast_guests'));
assert($node instanceof PodcastEpisode);
$guests = $node->get('field_podcast_guests')->referencedEntities();
$guests = $node->getGuests();
assert(is_array($guests));
assert(!is_null($guests[0]));
assert($guests[0] instanceof TermInterface);

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Drupal\opd_podcast\Entity;
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(): array {
return $this->get('field_podcast_guests')->referencedEntities();
}
}