-
Notifications
You must be signed in to change notification settings - Fork 3
/
islandora_base_theme.theme
274 lines (248 loc) · 11.5 KB
/
islandora_base_theme.theme
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* @file
* Functions to support theming in the SASS Starterkit subtheme.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_form_system_theme_settings_alter() for settings form.
*
* Replace Barrio setting options with subtheme ones.
*/
function islandora_base_theme_form_system_theme_settings_alter(&$form, FormStateInterface $form_state)
{
$form['components']['navbar']['bootstrap_barrio_navbar_top_background']['#options'] = array(
'bg-primary' => t('Primary') ,
'bg-secondary' => t('Secondary') ,
'bg-light' => t('Light') ,
'bg-dark' => t('Dark') ,
'bg-white' => t('White') ,
'bg-transparent' => t('Transparent') ,
);
$form['components']['navbar']['bootstrap_barrio_navbar_background']['#options'] = array(
'bg-primary' => t('Primary') ,
'bg-secondary' => t('Secondary') ,
'bg-light' => t('Light') ,
'bg-dark' => t('Dark') ,
'bg-white' => t('White') ,
'bg-transparent' => t('Transparent') ,
);
}
/**
* Implements hook_preprocess_breadcrumb() for overriding breadcrumbs.
*
* Adds the title to the end of the breadcrumb.
*/
function islandora_base_theme_preprocess_breadcrumb(&$variables)
{
try{
if ($variables['breadcrumb'])
{
$current_path = \Drupal::service('path.current')->getPath();
$current_path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
// Adding this for Breadcrumbs on Taxonomy term page list.
if (strpos($current_path_alias, '/taxonomy/term/') !== false) {
$taxonomy_obj = explode('/', $current_path);
$taxonomy_id = $taxonomy_obj[3];
$loadedterm = Term::load($taxonomy_id);
$vocabularyId = $loadedterm->bundle();
$variables['parent_taxonomy_term'] = $vocabularyId;
}
$request = \Drupal::request();
$route_match = \Drupal::routeMatch();
$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
// If node has a parent, set the link.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node and !($node instanceof \Drupal\node\NodeInterface)) {
// .../pages does not return node but does return NID. Retry loading node from NID.
$node = \Drupal::entityTypeManager()->getStorage('node')->load($node);
}
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->hasField('field_member_of') and !empty($node->get('field_member_of')->first())) {
try {
$nodeparent = $node->get('field_member_of')->first()->target_id;
$parent_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nodeparent);
$nodeparentlabel = $node->get('field_member_of')->entity->label();
$parent_node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeparent);
if( $parent_node and $parent_node->hasField('field_member_of') and !empty($parent_node->get('field_member_of')->first())) {
$grand_parent_node = $parent_node->get('field_member_of')->first()->target_id;
$grand_parent_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $grand_parent_node);
$grand_nodeparent_label = $parent_node->get('field_member_of')->entity->label();
}
}
catch (Exception $e) {
// Log the exception to watchdog.
watchdog_exception('type', $e);
}
if (!empty($nodeparent))
{
// Force order if current node is the same
if ($node == \Drupal::routeMatch()->getParameter('node')) {
if (isset($grand_parent_alias)) {
$variables['breadcrumb'][] = array('text' => $grand_nodeparent_label, 'url' => $grand_parent_alias);
}
$variables['breadcrumb'][] = array('text' => $nodeparentlabel, 'url' => $parent_alias);
} else {
if (isset($grand_parent_alias)) {
$variables['breadcrumb'][1] = array('text' => $grand_nodeparent_label, 'url' => $grand_parent_alias);
$variables['breadcrumb'][2] = array('text' => $nodeparentlabel, 'url' => $parent_alias);
} else {
$variables['breadcrumb'][1] = array('text' => $nodeparentlabel, 'url' => $parent_alias);
}
}
}
}
}
if (!empty($page_title))
{
$variables['breadcrumb'][] = ['text' => $page_title, 'attributes' => new Attribute(['class' => ['active']]) ];
}
// Fixes duplicated breadcrumbs
function remove_dup($linked_nodes) {
$tmp_list=array();
$modified_list=array();
for($n=0;$n<count($linked_nodes);$n++)
{
$tmp_list[]=serialize($linked_nodes[$n]);
}
$mat=array_unique($tmp_list);
for($n=0;$n<count($linked_nodes);$n++)
{
if (isset($mat[$n])) {
$modified_list[]=unserialize($mat[$n]);
}
}
if (($key = array_search(false, $modified_list)) !== false) {
unset($modified_list[$key]);
}
return $modified_list;
}
// dd($variables['breadcrumb']);
$variables['breadcrumb'] = remove_dup($variables['breadcrumb']);
}
}
catch (Exception $e) {
// Log the exception to watchdog.
watchdog_exception('type', $e);
}
}
/**
* Implements hook_preprocess_page() for overriding pages.
*
* Adds the iiif variable to the page twig file.
*/
function islandora_base_theme_preprocess_page(&$vars)
{
$vars['iiif_server_location'] = \Drupal::config('islandora_iiif.settings')->get('iiif_server');
}
/**
* Implements hook_preprocess_image() for overriding images.
*
* Adds the iiif variable to the image twig file.
*/
function islandora_base_theme_preprocess_image(&$variables)
{
$variables['iiif_server_location'] = \Drupal::config('islandora_iiif.settings')->get('iiif_server');
$variables['file_default_scheme'] = \Drupal::config('system.file')->get('default_scheme');
$variables['host'] = \Drupal::request()->getSchemeAndHttpHost();
// Get the object title for the file to use as alt text instead of the filename.
// Start by getting the filename and removing any query params from the uri.
$filename = \Drupal::service('file_system')->basename($variables['uri']);
$filename = strstr($filename, '?', TRUE) ?: $filename;
$filename = urldecode($filename);
// Look up the file entity.
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['filename' => $filename]);
$file = reset($files);
if ($file) {
// Look up its referencing media.
$utils = \Drupal::service('islandora.utils');
$media = $utils->getReferencingMedia($file->id());
$media = reset($media);
if ($media) {
// Try and look up the parent node.
// If the node does not exist, use the media title as a fallback.
$node = $utils->getParentNode($media);
$title = $node ? $node->getTitle() : $media->getName();
$variables['alt'] = $title;
$variables['attributes']['alt'] = $title;
}
}
// Ensure alt texts are less than 100 characters.
// This avoids 'Long Alternative Text' accessibility errors.
$variables['alt'] = substr($variables['alt'], 0, 100);
$variables['attributes']['alt'] = substr($variables['attributes']['alt'], 0, 100);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function islandora_base_theme_suggestions_field(&$variables)
{
$suggestions = [];
$element = $variables['element'];
$suggestions[] = 'field__' . $element['#field_type'];
$suggestions[] = 'field__' . $element['#field_name'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#bundle'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'];
$suggestions[] = 'field__' . $element['#entity_type'] . '__' . $element['#field_name'] . '__' . $element['#bundle'];
return $suggestions;
}
/**
* Implements hook_preprocess_media() for overriding download button.
*
* Adds the Islandora Access by terms as variable values to the media twig file.
*/
function islandora_base_theme_preprocess_media(&$variables)
{
$group_content = \Drupal\group\Entity\GroupContent::loadByEntity($variables['media']);
if (!empty($group_content)) {
$group = reset($group_content)->getGroup();
$permission_checker = \Drupal::service('group_permission.checker');
$variables['show_download_button'] = $permission_checker->hasPermissionInGroup('view download button', \Drupal::currentUser(), $group);
}
else {
$variables['show_download_button'] = TRUE;
}
}
/**
* Implements hook_preprocess_field() for overriding fields.
*
* Adds the iiif variable to the field twig file.
*/
function islandora_base_theme_preprocess_field(&$variables)
{
try {
// Fix youtube URLs for remote videos
if (isset($variables['items'][0]['content']['#attributes']['src'])) {
$URL_SOURCE = $variables['items'][0]['content']['#attributes']['src'];
$URL_YOUTUBE_MEDIA = 'https://www.youtube.com/media/oembed?url=https%3A//www.youtube.com/watch%3Fv%3D';
$URL_YOUTUBE_CORRECTED = 'https://www.youtube.com/embed/';
$URL_YOUTU_BE_URL = 'https://www.youtube.com/media/oembed?url=https%3A//youtu.be/';
// Fix: strip the value to it's simplest possible string for youtube to work.
if (str_starts_with($URL_SOURCE, $URL_YOUTUBE_MEDIA) || str_starts_with($URL_SOURCE, $URL_YOUTU_BE_URL)) {
$URL_SOURCE = str_replace($URL_YOUTU_BE_URL,$URL_YOUTUBE_CORRECTED,$URL_SOURCE);
$URL_SOURCE = str_replace($URL_YOUTUBE_MEDIA,$URL_YOUTUBE_CORRECTED,$URL_SOURCE);
// Strips out &max_width=0&max_height=0&hash=...
$URL_SOURCE = strstr(rawurldecode($URL_SOURCE), '&', true);
}
// Remove the embedded size upon save
$variables['items'][0]['content']['#attributes']['src'] = $URL_SOURCE;
}
if(isset($variables['element']['#view_mode'])) {
if($variables['element']['#view_mode'] == 'search_index') {
$show_button[] = 'false';
} else {
$show_button[] = 'true';
}
}
// If any are true set status to true.
$button_status = (in_array('true', $show_button) ? true : false);
// Pass the button status to twig file.
$variables['show_download_button'] = $button_status;
}
catch (Exception $e) {
// Log the exception to watchdog.
watchdog_exception('type', $e);
}
}