This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtide_site.tokens.inc
80 lines (71 loc) · 2.39 KB
/
tide_site.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* @file
* Tokens.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info_alter().
*/
function tide_site_token_info_alter(&$data) {
$data['tokens']['url']['path-no-site-prefix'] = [
'name' => t('Path without Site Prefix'),
'description' => t('The base path component without site prefix.'),
];
$data['tokens']['node']['publication-parent-prefix'] = [
'name' => t('Publication parent with prefix'),
'description' => t('The publication parent to use with linkit'),
];
}
/**
* Implements hook_tokens().
*/
function tide_site_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$language_manager = \Drupal::languageManager();
$url_options = ['absolute' => TRUE];
if (isset($options['langcode'])) {
$url_options['language'] = $language_manager->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = $language_manager->getCurrentLanguage()->getId();
}
if ($type == 'url' && !empty($data['url'])) {
/** @var \Drupal\Core\Url $url */
$url = $data['url'];
// To retrieve the correct path, modify a copy of the Url object.
$path_url = clone $url;
$path = '/';
// Ensure the URL is routed to avoid throwing an exception.
if ($url->isRouted()) {
$path .= $path_url->setAbsolute(FALSE)->setOption('fragment', NULL)->getInternalPath();
}
foreach ($tokens as $name => $original) {
switch ($name) {
case 'path-no-site-prefix':
$value = !($url->getOption('alias')) ? \Drupal::service('path_alias.manager')->getAliasByPath($path, $langcode) : $path;
$value = preg_replace("/^\/site\-(\d+)\//", '/', $value, 1);
$replacements[$original] = $value;
break;
}
}
}
if ($type == 'node' && !empty($data['node'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'publication-parent-prefix':
$node = $data['node'];
if ($node->getType() == 'publication_page') {
if ($node->hasField('field_publication') && !$node->get('field_publication')->isEmpty()) {
$text = $node->field_publication->entity->getTitle();
$value = "| Parent: " . $text;
}
$replacements[$original] = $value;
break;
}
}
}
}
return $replacements;
}