-
Notifications
You must be signed in to change notification settings - Fork 5
/
ad_integration.tokens.inc
94 lines (82 loc) · 2.36 KB
/
ad_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
<?php
/**
* @file
* Builds placeholder replacement tokens for ad-related data.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\taxonomy\TermInterface;
/**
* Implements hook_token_info().
*/
function ad_integration_token_info() {
$type = array(
'name' => t('Advertising'),
'description' => t('Tokens for Advertising data.'),
'needs-data' => 'advertising',
);
// Core comment tokens.
$ad['adsc_unit1'] = array(
'name' => t("Ad unit 1"),
'description' => t("Ad unit 1."),
);
$ad['adsc_unit2'] = array(
'name' => t("Ad unit 2"),
'description' => t("Ad unit 2."),
);
$ad['adsc_unit3'] = array(
'name' => t("Ad unit 3"),
'description' => t("Ad unit 3."),
);
$ad['adsc_keyword'] = array(
'name' => t("Ad keyword"),
'description' => t("Komma seperated list of ad keywords."),
);
$ad['adsc_mode'] = array(
'name' => t("Ad mode"),
'description' => t("Ad mode."),
);
return array(
'types' => array('advertising' => $type),
'tokens' => array(
'advertising' => $ad,
),
);
}
/**
* Implements hook_tokens().
*/
function ad_integration_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'advertising') {
$lookupFrom = 'currentRoute';
if (isset($data['entity']) && $data['entity'] instanceof ContentEntityInterface) {
$lookupFrom = 'entity';
}
elseif (isset($data['term']) && $data['term'] instanceof TermInterface) {
$lookupFrom = 'term';
}
/** @var \Drupal\ad_integration\AdIntegrationLookupInterface $lookup */
$lookup = \Drupal::service('ad_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;
}