-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemename.theme
89 lines (71 loc) · 3.29 KB
/
themename.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
<?php
/**
* @file
* Functions to support theming in the THEMENAME theme.
*/
/**
* Implements hook_preprocess_html().
*/
function themename_preprocess_html(&$variables) {
// Add a body class based on the route, so scss containers can be added to non-node pages.
$route_name = \Drupal::request()->attributes->get('_route');
$variables['attributes']['class'] = 'route--'. \Drupal\Component\Utility\Html::cleanCssIdentifier($route_name, [ '.' => '-', '_' => '-']);
}
/**
* Implements hook_preprocess_paragraph().
*/
function themename_preprocess_paragraph(&$variables) {
// If a paragraph has a boolean field to hide the heading, provide a class for css.
if ($variables['paragraph']->hasField('field_hide_heading') && in_array($variables['paragraph']->get('field_hide_heading')->getString(), ["1", "On"])) {
$variables['attributes']['class'][] = "paragraph--hidden-heading";
}
// If a paragraph has a boolean field to show an alternate style, provide a class for css.
if ($variables['paragraph']->hasField('field_alternate_style') && in_array($variables['paragraph']->get('field_alternate_style')->getString(), ["1", "On"])) {
$variables['attributes']['class'][] = "paragraph--alternate-style";
}
}
/**
* Implements hook_preprocess_views_view().
*/
function themename_preprocess_views_view(&$variables) {
// If this view is displaying rendered entities, pass the view mode to the template to add a class.
if (isset($variables['view']->rowPlugin->options['view_mode'])) {
$variables['view_mode'] = $variables['view']->rowPlugin->options['view_mode'];
}
}
/**
* Implements hook_preprocess_image().
*/
function themename_preprocess_image(&$variables) {
// Don't print the alt text for an image if it appears to be a filename.
if (in_array(substr($variables['alt'], strrpos($variables['alt'], '.') + 1), ['jpg', 'jpeg', 'png', 'gif'])) {
$variables['attributes']['alt'] = '';
$variables['attributes']['title'] = '';
}
}
/**
* Implements template_preprocess_image_formatter().
*/
function themename_preprocess_image_formatter(&$variables) {
// Output focal point values as an object-position style in the img element to allow css to pseudo-crop it with object-fit: cover.
if (isset($variables['item']->target_id)) {
if ($file = \Drupal::service('entity_type.manager')->getStorage('file')->load($variables['item']->target_id)) {
if ($crop_type = \Drupal::config('focal_point.settings')->get('crop_type')) {
if ($crop = \Drupal\crop\Entity\Crop::findCrop($file->getFileUri(), $crop_type)) {
$anchor = \Drupal::service('focal_point.manager')
->absoluteToRelative($crop->x->value, $crop->y->value, $variables['item']->width, $variables['item']->height);
$variables['image']['#attributes']['style'] = "object-position: {$anchor['x']}% {$anchor['y']}%;";
}
}
}
}
}
/**
* Implements template_preprocess_responsive_image_formatter().
*/
function themename_preprocess_responsive_image_formatter(&$variables) {
// We need to use the same code as a regular image formatter, so just call that, and then move the result to the right place.
themename_preprocess_image_formatter($variables);
$variables['responsive_image']['#attributes']['style'] = $variables['image']['#attributes']['style'];
unset($variables['image']);
}