oliverdavies.uk/modules/opd_podcast/opd_podcast.module

92 lines
2.5 KiB
PHP
Raw Normal View History

2025-05-25 19:48:07 +01:00
<?php
declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
2025-05-25 19:48:07 +01:00
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;
2025-05-25 19:48:07 +01:00
2025-05-29 18:12:17 +01:00
/**
* @param array<non-empty-string, array<non-empty-string, array{}>> $links
* @param array<non-empty-string, mixed> $context
*/
2025-05-25 19:48:07 +01:00
function opd_podcast_node_links_alter(array &$links, NodeInterface $entity, array &$context): void {
if ($entity->bundle() !== 'podcast_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<non-empty-string, array{description: TranslatableMarkup, name: TranslatableMarkup}[]>, types: array<non-empty-string, 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 NodeInterface);
assert($node->hasField('field_podcast_guests'));
$guests = $node->get('field_podcast_guests')->referencedEntities();
assert(is_array($guests));
assert(!is_null($guests[0]));
assert($guests[0] instanceof TermInterface);
$replacements[$original] = $guests[0]->label();
break;
}
}
}
return $replacements;
}