-
Notifications
You must be signed in to change notification settings - Fork 0
/
gall.theme
489 lines (416 loc) · 13.7 KB
/
gall.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* @file
* Functions to support theming in the gall theme.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\node\Entity\Node;
use Drupal\Component\Utility\Html;
use Drupal\Core\Link;
use Drupal\Core\Url;
define('GALL_VIEWS_ITEMS_PER_ROW', 4);
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*/
function gall_preprocess_html(&$variables) {
/* Add class to html tag */
//$variables['html_attributes']->addClass('no-js');
// Don't display the site name twice on the front page (and potentially others)
/*if (isset($variables['head_title_array']['title']) && isset($variables['head_title_array']['name']) && ($variables['head_title_array']['title'] == $variables['head_title_array']['name'])) {
$variables['head_title'] = $variables['head_title_array']['name'];
}*/
}
/**
* Implements hook_page_attachments_alter
*/
function gall_page_attachments_alter(array &$page) {
// Disabling browser zoom on mobile devices.
/*foreach ($page['#attached']['html_head'] as &$meta_arr) {
if (array_search('viewport', $meta_arr)) {
$meta_arr[0]['#attributes']['content'] .= ', maximum-scale=1.0, user-scalable=0';
}
}*/
// Tell IE to use latest rendering engine (not to use compatibility mode).
/*$ie_edge = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'X-UA-Compatible',
'content' => 'IE=edge',
),
);
$page['#attached']['html_head'][] = [$ie_edge, 'ie_edge'];*/
}
/**
* Implements hook_preprocess_page() for PAGE document templates.
*/
function gall_preprocess_page(&$variables) {
$svg = file_get_contents(\Drupal::service('extension.list.theme')->getPath('gall') . '/images/icons.svg');
$variables['svg_icons'] = $svg;
}
/**
* Implements template_preprocess_maintenance_page().
*/
function gall_preprocess_maintenance_page(&$variables) {
$variables['logo'] = str_replace('.svg', '.png', $variables['logo']);
$variables['site_name'] = '';
}
/**
* Implements hook_theme_suggestions_page_alter().
*/
function gall_theme_suggestions_page_alter(array &$suggestions, array $variables) {
}
/**
* Implements hook_theme_suggestions_node_alter().
*/
function gall_theme_suggestions_node_alter(array &$suggestions, array $variables) {
/*$node = $variables['elements']['#node'];
if ($variables['elements']['#view_mode'] == "full") {
}*/
}
/**
* Implements hook_preprocess_HOOK() for Block document templates.
*/
function gall_preprocess_block(&$variables) {
switch ($variables['attributes']['id']) {
case 'block-gall-branding':
case 'block-galledit-branding':
// Use the PNG logo.
$variables['site_logo'] = str_replace('.svg', '.png', $variables['site_logo']);
break;
}
// Add a class to all block headings.
$variables['title_attributes']['class'][] = 'block-title';
// Link block titles to their view pages.
$link_title_blocks = array(
'views_block:exhibitions_new-block_1',
'views_block:galleries_new-block_1',
'views_block:exhibitions_this_gallery-current',
'views_block:exhibitions_this_gallery-upcoming',
'views_block:exhibitions_this_gallery-past',
'views_block:exhibitions_this_artist-current',
'views_block:exhibitions_this_artist-upcoming',
'views_block:exhibitions_this_artist-past',
);
if (in_array($variables['plugin_id'], $link_title_blocks)) {
$display = $variables['content']['#view']->current_display;
// If the view has any results, link the block to the view page.
if (!empty($variables['content']['#view']->result)) {
$title = '';
if (!empty($variables['content']['#view']->build_info['substitutions'])) {
$argument = $variables['content']['#view']->build_info['substitutions'];
if (!empty($argument['{{ arguments.nid }}'])) {
$title = $argument['{{ arguments.nid }}'];
}
elseif (!empty($argument['{{ arguments.tid }}'])) {
$title = $argument['{{ arguments.tid }}'];
}
}
$url = _gall_view_block_page_url($variables['plugin_id'], $display, $title);
if (!empty($url)) {
$link = Link::fromTextAndUrl($variables['label']['#markup'], $url);
$variables['label'] = $link->toRenderable();
}
}
}
}
/**
* Get the relevant page URL for a block view.
*
* @param string $plugin_id
* The block plugin ID.
* @param string $display
* The view display.
* @param string $title
* The title of the item being used to filter the view.
*
* @return \Drupal\Core\Url
* The URL of the full page view.
*/
function _gall_view_block_page_url($plugin_id, $display = '', $title = '') {
$url = '';
$argument = strtolower(Html::cleanCssIdentifier($title));
switch ($plugin_id) {
case 'views_block:exhibitions_this_gallery-current':
$url = '/exhibitions/' . $argument;
break;
case 'views_block:exhibitions_this_gallery-upcoming':
case 'views_block:exhibitions_this_gallery-past':
$url = '/exhibitions/' . $display . '/' . $argument;
break;
case 'views_block:exhibitions_this_artist-current':
case 'views_block:exhibitions_this_artist-upcoming':
case 'views_block:exhibitions_this_artist-past':
$url = '/exhibitions/artist/' . $argument . '/' . $display;
break;
case 'views_block:galleries_this_artist-current':
$url = '/galleries/' . $display . '/' . $argument;
break;
case 'views_block:exhibitions_new-block_1':
$url = '/exhibitions/new';
break;
case 'views_block:galleries_new-block_1':
$url = '/galleries/new';
break;
}
return Url::fromUserInput($url);
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function gall_theme_suggestions_field_alter(array &$suggestions, array $variables) {
$element = $variables['element'];
$suggestions[] = 'field__' . $element['#view_mode'];
$suggestions[] = 'field__' . $element['#view_mode'] . '__' . $element['#field_name'];
if ($element['#field_name'] == 'field_image') {
$suggestions[] = 'field__' . 'field_images';
}
}
/**
* Implements hook_theme_suggestions_alter().
*/
function gall_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
// dpm($hook);
}
/**
* Implements template_preprocess_views_view_fields().
*/
function gall_preprocess_views_view_fields(&$variables) {
$term_views = _gall_tiles_term_views();
$view_id = $variables['view']->id();
if (in_array($view_id, $term_views)) {
$term_id = $variables['row']->_entity->id();
$variables['term_path'] = Url::fromRoute('entity.taxonomy_term.canonical', array(
'taxonomy_term' => $term_id,
));
}
}
/**
* Implements hook_theme_suggestions_views_view_fields_alter().
*/
function gall_theme_suggestions_views_view_fields_alter(array &$suggestions, array $variables) {
// Set up views to use the tiles template.
$view_id = $variables['view']->id();
$tiles_views = _gall_tiles_node_views();
if (in_array($view_id, $tiles_views)) {
$suggestions[] = 'views_view_fields__tiles';
}
$term_views = _gall_tiles_term_views();
if (in_array($view_id, $term_views)) {
$suggestions[] = 'views_view_fields__tags';
}
$suggestions[] = 'views_view_fields__' . $view_id;
}
/**
* Get a list of node views that should follow the tiles pattern.
*
* @return array
* A list of view IDs to which the tiles styles should apply.
*/
function _gall_tiles_node_views() {
return array(
'exhibitions',
'exhibitions_a_z',
'exhibitions_new',
'exhibitions_related',
'exhibitions_this_gallery',
'exhibitions_this_artist',
'exhibitions_this_artist_pages',
'galleries_a_z',
'galleries_new',
'galleries_this_artist',
'galleries_by_tag',
);
}
/**
* Get a list of term views that should follow the tiles pattern.
*
* @return array
* List of view IDs.
*/
function _gall_tiles_term_views() {
return array(
'artists',
'artists_new',
'artists_tag',
'tags',
);
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function gall_theme_suggestions_fieldset_alter(array &$suggestions, array $variables) {
/*$element = $variables['element'];
if (isset($element['#attributes']['class']) && in_array('form-composite', $element['#attributes']['class'])) {
$suggestions[] = 'fieldset__form_composite';
}*/
}
/**
* Implements hook_preprocess_node().
*/
function gall_preprocess_node(&$variables) {
// Set up node type specific preprocess functions.
$node_type = $variables['node']->bundle();
$function = __FUNCTION__ . '_' . $node_type;
if (function_exists($function)) {
$function($variables);
}
switch ($node_type) {
case 'exhibition':
case 'gallery':
_gall_format_admission($variables);
break;
}
}
/**
* Specific preprocess function for gallery nodes.
*/
function gall_preprocess_node_gallery(&$variables) {
if ($variables['view_mode'] == 'full') {
// If there's no body, add a placeholder.
$body = $variables['node']->get('body')->getValue();
if (empty($body)) {
$edit_link_object = Link::createFromRoute('edit the listing', 'entity.node.edit_form', array('node' => $variables['node']->id()));
$edit_link = $edit_link_object->toString();
$variables['content']['body'] = array(
'#markup' => "<div class='field--name-body'><p>We don't currently have a description for this gallery.</p>" .
"<p>Please " . $edit_link . " to add more information.</p></div>",
);
}
// If there are no artists, add a placeholder.
$artists = $variables['node']->get('artists')->getValue();
if (empty($artists)) {
$variables['content']['artists'] = _gall_tags_placeholder('Artists');
}
// If there are no tags, add a placeholder.
$tags = $variables['node']->get('tags')->getValue();
if (empty($tags)) {
$variables['content']['tags'] = _gall_tags_placeholder('Tags');
}
// Hide opening times if empty.
$opening_times = $variables['node']->get('field_openingtimes')->getValue();
if (empty($opening_times)) {
unset($variables['content']['field_openingtimes']);
}
}
}
/**
* Prepare placeholder text for a tags block.
*
* @param string $title
* The title of the tags block.
*
* @return array
* Render array of markup.
*/
function _gall_tags_placeholder($title) {
$body = 'There are no ' . strtolower($title) . ' currently listed - why not add some?';
return array(
'#markup' => '<div class="field"><div class="field__label">' . $title . '</div><div class="field__items">' . $body . '</div></div>',
);
}
/**
* Format admission fees.
*
* @param array $variables
* The variables available in hook_preprocess_node().
*/
function _gall_format_admission(&$variables) {
$node = $variables['node'];
// Show zero admission as free.
$standard_admission = $node->get('field_admission_std');
$admission_fee = $standard_admission->value;
if (!empty($admission_fee) && $admission_fee == 0) {
$variables['admission'] = array(
'#markup' => t('Free'),
);
}
}
/**
* Implements hook_theme_suggestions_views_view_alter().
*/
function gall_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
}
/**
* Implements hook_preprocess_form().
*/
function gall_preprocess_form(&$variables) {
//$variables['attributes']['novalidate'] = 'novalidate';
}
/**
* Implements hook_preprocess_select().
*/
function gall_preprocess_select(&$variables) {
//$variables['attributes']['class'][] = 'select-chosen';
}
/**
* Implements hook_preprocess_field().
*/
function gall_preprocess_field(&$variables, $hook) {
switch ($variables['element']['#field_name']) {
case 'field_image':
case 'field_images':
// If this is the default image, add a class.
$images = $variables['element']['#items']->referencedEntities();
if (empty($images)) {
$variables['attributes']['class'][] = 'image__default';
}
break;
}
}
/**
* Implements hook_preprocess_details().
*/
function gall_preprocess_details(&$variables) {
/*$variables['attributes']['class'][] = 'details';
$variables['summary_attributes']['class'] = 'summary';*/
}
/**
* Implements hook_theme_suggestions_details_alter().
*/
function gall_theme_suggestions_details_alter(array &$suggestions, array $variables) {
}
/**
* Implements hook_preprocess_menu_local_task().
*/
function gall_preprocess_menu_local_task(&$variables) {
//$variables['element']['#link']['url']->setOption('attributes', array('class'=>'rounded'));
}
/**
* Implements hook_views_pre_render().
*/
function gall_views_pre_render($view) {
// Limit the distance to 2 decimal places.
if (!empty($view->element['#name']) && ($view->element['#name'] == 'galleries_nearby')) {
foreach ($view->result as $row) {
if (!empty($row->node__field_location_field_location_proximity1)) {
$row->node__field_location_field_location_proximity1 = number_format($row->node__field_location_field_location_proximity1, 2);
}
}
}
}
/**
* Implements template_preprocess_views_view_unformatted().
*/
function gall_preprocess_views_view_unformatted(&$variables) {
$view = $variables['view'];
$view_id = $view->id();
// Sort out tiles views.
$tiles_views = array_merge(_gall_tiles_node_views(), _gall_tiles_term_views());
if (in_array($view_id, $tiles_views)) {
// Add dummy rows so that flexbox looks nice.
$remainder = count($variables['view']->result) % GALL_VIEWS_ITEMS_PER_ROW;
$rows_to_add = GALL_VIEWS_ITEMS_PER_ROW - $remainder;
if ($remainder && $rows_to_add) {
$variables['extra_rows'] = $rows_to_add;
}
}
}
/**
* Implementes hook_form_FORM_ID_alter().
*/
function gall_form_search_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add a placeholder to the search input.
$form['keys']['#attributes']['placeholder'] = t('Search');
}