-
Notifications
You must be signed in to change notification settings - Fork 0
/
mespronos.tokens.inc
194 lines (172 loc) · 4.16 KB
/
mespronos.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @file
* Builds placeholder replacement tokens for node-related data.
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\mespronos\Entity\Day;
use Drupal\mespronos\Entity\Game;
use Drupal\mespronos\Entity\League;
/**
* Implements hook_token_info().
*/
function mespronos_token_info() {
return [
'types' => [
'league' => [
'name' => t('League'),
'description' => t('Tokens related to a league.'),
],
'game' => [
'name' => t('Game'),
'description' => t('Tokens related to a game.'),
],
'day' => [
'name' => t('Day'),
'description' => t('Tokens related to a day.'),
],
],
'tokens' => [
'league' => _mespronos_token_info_league(),
'game' => _mespronos_token_info_game(),
'day' => _mespronos_token_info_day(),
],
];
}
function _mespronos_token_info_league() {
$tokens = [
'id' => [
'name' => t('League ID'),
'description' => t('The League ID.'),
],
'name' => [
'name' => t('League name'),
],
'sport' => [
'name' => t('Sport name'),
],
'logo' => [
'name' => t('League logo'),
],
];
return $tokens;
}
function _mespronos_token_info_game() {
$tokens = [
'id' => [
'name' => t('League ID'),
],
'name' => [
'name' => t('Game label'),
],
'league' => [
'name' => t('League label'),
],
'day' => [
'name' => t('Day label'),
],
'league_logo' => [
'name' => t('League logo'),
],
];
return $tokens;
}
function _mespronos_token_info_day() {
$tokens = [
'id' => [
'name' => t('Day ID'),
],
'name' => [
'name' => t('Day label'),
],
'league' => [
'name' => t('League label'),
],
'league_logo' => [
'name' => t('League logo'),
],
];
return $tokens;
}
/**
* Implements hook_tokens().
*/
function mespronos_tokens($type, $tokens, array $data) {
$replacements = [];
if ($type === 'entity' && isset($data['entity_type'])) {
switch ($data['entity_type']) {
case 'league':
_mespronos_tokens_league($tokens, $data['entity'], $replacements);
break;
case 'game':
_mespronos_tokens_game($tokens, $data['entity'], $replacements);
break;
case 'day':
_mespronos_tokens_day($tokens, $data['entity'], $replacements);
break;
}
}
return $replacements;
}
function _mespronos_tokens_league($tokens, League $league, &$replacements) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $league->id();
break;
case 'name':
$replacements[$original] = $league->getTheName();
break;
case 'sport':
$replacements[$original] = $league->getSport()->label();
break;
case 'logo':
$replacements[$original] = $league->getLogoUrl();
break;
}
}
}
function _mespronos_tokens_game($tokens, Game $game, &$replacements) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $game->id();
break;
case 'name':
$replacements[$original] = $game->labelTeams();
break;
case 'day':
$day = $game->getDay();
$replacements[$original] = $day->label();
break;
case 'league':
$league = $game->getLeague();
$replacements[$original] = $league->label();
break;
case 'league_logo':
$league = $game->getLeague();
$replacements[$original] = $league->getLogoUrl();
break;
}
}
}
function _mespronos_tokens_day($tokens, Day $day, &$replacements) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $day->id();
break;
case 'name':
$replacements[$original] = $day->label();
break;
case 'league':
$league = $day->getLeague();
$replacements[$original] = $league->label();
break;
case 'league_logo':
$league = $day->getLeague();
$replacements[$original] = $league->getLogoUrl();
break;
}
}
}