-
Notifications
You must be signed in to change notification settings - Fork 14
/
oe_content.tokens.inc
49 lines (39 loc) · 1.1 KB
/
oe_content.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* @file
* Builds placeholder replacement tokens for oe_content content types.
*/
declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function oe_content_token_info() {
// Short title fallback token.
$node['short-title-fallback'] = [
'name' => t('Short title fallback'),
'description' => t('Short title field in case it is not empty or the title field otherwise.'),
];
return [
'tokens' => ['node' => $node],
];
}
/**
* Implements hook_tokens().
*/
function oe_content_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type !== 'node' || empty($data['node'])) {
return [];
}
/** @var \Drupal\node\NodeInterface $node */
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'short-title-fallback':
$replacements[$original] = $node->get('oe_content_short_title')->isEmpty() ? $node->getTitle() : $node->get('oe_content_short_title')->value;
break;
}
}
return $replacements;
}