Newer
Older
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
<?php
/**
* Implements hook_token_info().
*/
function customtokens_token_info() {
$type = [
'name' => t('Custom Token'),
'description' => t('Tokens for custom things.'),
];
$node['landingct'] = [
'name' => t("Research Landing Page"),
'description' => t('The node\'s Research Landing Page'),
];
return [
'types' => ['customtoken' => $type],
'tokens' => ['customtoken' => $node],
];
}
/**
* Implements hook_tokens().
*/
function customtokens_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$nids = \Drupal::entityQuery('node')->condition('type','research_landing_page')->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$lid = array_shift(array_keys($nodes));
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$lid);
if ($type == 'customtoken' && !empty($data['node'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'landingct':
$replacements[$original] = $alias;
break;
}
}
}
return $replacements;
}