2025-05-25 19:48:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2025-05-29 23:38:09 +01:00
|
|
|
use Drupal\Core\Render\BubbleableMetadata;
|
|
|
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
2025-05-25 19:48:07 +01:00
|
|
|
use Drupal\node\NodeInterface;
|
2025-06-12 02:10:08 +01:00
|
|
|
use Drupal\opd_podcast\Episode;
|
|
|
|
use Drupal\opd_podcast\Guest;
|
2025-05-25 19:48:07 +01:00
|
|
|
|
2025-06-12 02:10:08 +01:00
|
|
|
/**
|
|
|
|
* 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'])) {
|
2025-06-12 02:10:08 +01:00
|
|
|
$bundles['node'][Episode::NODE_TYPE]['class'] = Episode::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($bundles['taxonomy_term'])) {
|
|
|
|
$bundles['taxonomy_term'][Guest::TERM_TYPE]['class'] = Guest::class;
|
2025-06-12 02:10:08 +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 {
|
2025-06-12 02:10:08 +01:00
|
|
|
if (!$entity instanceof Episode) {
|
2025-05-25 19:48:07 +01:00
|
|
|
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';
|
|
|
|
}
|
2025-05-29 23:38:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_token_info().
|
|
|
|
*
|
2025-05-30 00:43:11 +01:00
|
|
|
* @return array{tokens: array{opd-podcast: array{description: TranslatableMarkup, name: TranslatableMarkup}[]}, types: array{opd-podcast: array{description: TranslatableMarkup, name: TranslatableMarkup}}}
|
2025-05-29 23:38:09 +01:00
|
|
|
*/
|
|
|
|
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;
|
2025-06-12 02:10:08 +01:00
|
|
|
assert($node instanceof Episode);
|
2025-05-29 23:38:09 +01:00
|
|
|
|
2025-06-12 02:10:08 +01:00
|
|
|
$replacements[$original] = strval($node->getGuests());
|
2025-05-29 23:38:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|
|
|
|
|