-
Notifications
You must be signed in to change notification settings - Fork 0
/
scholartokens.module
189 lines (174 loc) · 5.5 KB
/
scholartokens.module
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
<?php
/**
* @file
* Primary module hooks for ScholarTokens module.
*/
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function scholartokens_token_info() {
$type = [
'name' => t('Scholar Tokens'),
'description' => t('Supplemental Tokens for Islandora Scholar objects.'),
];
$node['scholar_author'] = [
'name' => t("Author"),
'description' => t("The citation's filtered Contributor linked agents"),
];
$node['scholar_date_issued'] = [
'name' => t("Date Issued"),
'description' => t("The citation's Issued Date"),
];
$node['scholar_start_page'] = [
'name' => t("Start Page"),
'description' => t("The citation's starting page"),
];
$node['scholar_end_page'] = [
'name' => t("End Page"),
'description' => t("The citation's end page"),
];
return [
'types' => ['scholartokens' => $type],
'tokens' => ['scholartokens' => $node],
];
}
/**
* Implements hook_tokens().
*/
function scholartokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$config = \Drupal::config('scholartokens.settings');
$replacements = [];
if ($type == 'scholartokens' && !empty($data['node'])) {
if (!is_array($tokens) || empty($tokens)) {
\Drupal::logger('scholartokens')
->alert('Tokens not correct format: @tokens', [
'@tokens' => print_r($tokens, 1),
]);
return;
}
foreach ($tokens as $name => $original) {
switch ($name) {
case 'scholar_author':
$allowed = ['relators:aut', 'relators:cre'];
$replacement = scholartokens_get_term_with_rel_type($data['node'], $config->get('contributor_field'), $allowed, TRUE);
$replacements[$original] = $replacement;
break;
case 'scholar_date_issued':
$node = $data['node'];
$replacement = scholartokens_format_date($node);
$replacements[$original] = $replacement;
break;
case 'scholar_start_page':
$node = $data['node'];
$replacement = scholartokens_page_range($node, 'reset');
$replacements[$original] = trim($replacement);
break;
case 'scholar_end_page':
$node = $data['node'];
$replacement = scholartokens_page_range($node, 'end');
$replacements[$original] = trim($replacement);
break;
}
}
}
return $replacements;
}
/**
* Date formatter.
*
* @param object $node
* A core drupal node object.
*/
function scholartokens_format_date($node) {
$config = \Drupal::config('scholartokens.settings');
$field = $config->get('field');
$original = $node->get($field)->value;
if (!$original) {
return '';
}
$forbidden = ['?', '%', '~'];
$original = str_replace($forbidden, '', $original);
$date_parts = explode('T', $original);
$original = $date_parts[0];
$date_parts = explode('-', $original);
if (count($date_parts) == 1) {
$replacement = $date_parts[0];
}
if (count($date_parts) == 2) {
$replacement = $date_parts[0];
}
if (count($date_parts) == 3) {
$date_parts = array_map(function ($value) {
// Ltrim removes leading zeros.
return ltrim($value, '0');
}, $date_parts);
$replacement = implode('/', $date_parts);
}
return $replacement;
}
/**
* Parses date range.
*/
function scholartokens_page_range($node, $function) {
$config = \Drupal::config('scholartokens.settings');
$splitters = [',', '_', '-'];
$field = $config->get('range_field');
$original = $node->get($field)->value;
if (!$original) {
return '';
}
$original = str_replace($splitters, '|', $original);
$range_parts = explode('|', $original);
return $function($range_parts);
}
/**
* Helper function to load values for a taxonomy term with a relationship type.
*
* @param object $node
* A core drupal node object.
* @param string $field_name
* The name of the node's field to check for the specific relationship.
* @param array $relation_types
* Optional values to check the rel_type of the taxonomy term against. When
* not provided, any terms returned for the field will match.
* @param bool $remove_comma
* Will flip the string parts of a CSV.
*
* @return string
* The tokenized value for the given data.
*/
function scholartokens_get_term_with_rel_type($node, $field_name, array $relation_types = [], $remove_comma = FALSE) {
$settings = \Drupal::config('metatag.settings');
$separator = $settings->get('separator');
$separator = ($separator) ? $separator : ',';
$matches = [];
$field = ($node->hasField($field_name) ? $node->get($field_name) : NULL);
if (is_object($field)) {
$tids = $field->getValue();
foreach ($tids as $tid) {
// Inspect the taxonomy term.
if (
is_array($tid) &&
array_key_exists('target_id', $tid)
) {
if ($relation_types && array_key_exists('rel_type', $tid)) {
if (in_array($tid['rel_type'], $relation_types)) {
$term = Term::load($tid['target_id']);
if ($term) {
$matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName();
}
}
}
else {
$term = Term::load($tid['target_id']);
if ($term) {
$matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName();
}
}
}
}
}
return implode($separator, $matches);
}