Return a token with the name of a single guest
This commit is contained in:
parent
52c5eda4a5
commit
487190cb12
7 changed files with 215 additions and 0 deletions
|
@ -2,7 +2,10 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Drupal\Core\Render\BubbleableMetadata;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
|
||||
/**
|
||||
* @param array<non-empty-string, array<non-empty-string, array{}>> $links
|
||||
|
@ -23,3 +26,66 @@ function opd_podcast_node_links_alter(array &$links, NodeInterface $entity, arra
|
|||
$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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,8 @@
|
|||
langcode: en
|
||||
status: true
|
||||
dependencies: { }
|
||||
name: 'Podcast guest'
|
||||
vid: podcast_guest
|
||||
description: null
|
||||
weight: 0
|
||||
new_revision: false
|
|
@ -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
|
75
modules/opd_podcast/tests/src/Kernel/PodcastTokenTest.php
Normal file
75
modules/opd_podcast/tests/src/Kernel/PodcastTokenTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?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\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'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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' => 'podcast_guest',
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string[] $guestNames
|
||||
*/
|
||||
private function createPodcastNode(array $guestNames): NodeInterface {
|
||||
return $this->createNode([
|
||||
'field_podcast_guests' => $this->createGuestTerms($guestNames),
|
||||
'title' => '::title::',
|
||||
'type' => 'podcast_episode',
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue