This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ivw_integration.tokens.inc
134 lines (118 loc) · 3.46 KB
/
ivw_integration.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* @file
* Builds placeholder replacement tokens for ivw-related data.
*/
use Drupal\Component\Utility\Xss;
use Drupal\taxonomy\TermInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
/**
* Implements hook_token_info().
*/
function ivw_integration_token_info() {
$type = [
'name' => t('IVW'),
'description' => t('Tokens for IVW data.'),
'needs-data' => 'ivw',
];
// Core comment tokens.
$ivw['site'] = [
'name' => t("IVW site name"),
'description' => t("Site name as provided by IVW."),
];
$ivw['responsive'] = [
'name' => t("Responsive flag"),
'description' => t("Has the value 1 if the site is cionfigured to be responsive, else it is set to 0."),
];
$ivw['mobile_width'] = [
'name' => t("Mobile width"),
'description' => t("The maximum width in pixels until a devive is considered to be mobile."),
];
$ivw['offering'] = [
'name' => t("Offering"),
'description' => t("An identifier for the offering on this site."),
];
$ivw['language'] = [
'name' => t("Content language"),
'description' => t("Content language ID."),
];
$ivw['format'] = [
'name' => t("Format"),
'description' => t("The content format ID."),
];
$ivw['creator'] = [
'name' => t("Creator"),
'description' => t("The content creator ID."),
];
$ivw['homepage'] = [
'name' => t('Hompage'),
'description' => t('Denotes, if the content is the homepage of the offering.'),
];
$ivw['delivery'] = [
'name' => t("Deliver"),
'description' => t("Delivery type ID."),
];
$ivw['app'] = [
'name' => t("App flag"),
'description' => t("Flags if the offering is an mobile app."),
];
$ivw['paid'] = [
'name' => t("paid flag"),
'description' => t("Flags paid content."),
];
$ivw['content'] = [
'name' => t("Content"),
'description' => t("Content category identifier."),
];
$ivw['frabo'] = [
'name' => t("Frabo control"),
'description' => t("The frabo control variable."),
];
$ivw['frabo_mobile'] = [
'name' => t("Frabo mobile control"),
'description' => t("The frabo mobile control variable."),
];
return [
'types' => ['ivw' => $type],
'tokens' => [
'ivw' => $ivw,
],
];
}
/**
* Implements hook_tokens().
*/
function ivw_integration_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$sanitize = !empty($options['sanitize']);
$replacements = [];
if ($type == 'ivw') {
$lookupFrom = 'currentRoute';
if (isset($data['entity']) && $data['entity'] instanceof ContentEntityInterface) {
$lookupFrom = 'entity';
}
elseif (isset($data['term']) && $data['term'] instanceof TermInterface) {
$lookupFrom = 'term';
}
/** @var \Drupal\ivw_integration\IvwLookupServiceInterface $lookup */
$lookup = \Drupal::service('ivw_integration.lookup');
foreach ($tokens as $name => $original) {
switch ($lookupFrom) {
case 'currentRoute':
$replacement = $lookup->byCurrentRoute($name);
break;
case 'entity':
$replacement = $lookup->byEntity($name, $data['entity']);
break;
case 'term':
$replacement = $lookup->byTerm($name, $data['term']);
break;
default:
$replacement = NULL;
break;
}
$replacements[$original] = $sanitize ? Xss::filter($replacement) : $replacement;
}
}
return $replacements;
}