-
Notifications
You must be signed in to change notification settings - Fork 0
/
ambientimpact_media.module
160 lines (151 loc) · 5.53 KB
/
ambientimpact_media.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
<?php
use Drupal\Component\Utility\NestedArray;
/**
* Prepares variables for image formatter templates.
*
* Default template: image-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
*
* - item: An ImageItem object.
*
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
*
* - image_style: An optional image style.
*
* - url: An optional \Drupal\Core\Url object.
*
* @see \Drupal\ambientimpact_media\Plugin\AmbientImpact\Component\AnimatedGIFToggle::preprocessImageFormatter()
* Variables are passed to this for modification.
*/
function ambientimpact_media_preprocess_image_formatter(&$variables) {
\Drupal::service('plugin.manager.ambientimpact_component')
->getComponentInstance('animated_gif_toggle')
->preprocessImageFormatter($variables);
}
/**
* Prepares variables for image link to image style formatter templates.
*
* Default template: image-formatter-link-to-image-style-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
*
* - item: An ImageItem object.
*
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
*
* - image_style: An optional image style.
*
* - url: A \Drupal\Core\Url object.
*
* - link_attributes: An array of attributes to add to the link if 'url' is
* set.
*
* @see \Drupal\ambientimpact_media\Plugin\AmbientImpact\Component\AnimatedGIFToggle::preprocessImageFormatter()
* Variables are passed to this for modification.
*/
function ambientimpact_media_preprocess_image_formatter_link_to_image_style_formatter(
array &$variables
) {
if (!empty($variables['url_attributes'])) {
$variables['link_attributes'] = NestedArray::mergeDeep(
$variables['url_attributes'],
$variables['link_attributes']
);
}
\Drupal::service('plugin.manager.ambientimpact_component')
->getComponentInstance('animated_gif_toggle')
->preprocessImageFormatter($variables);
}
/**
* Prepares variables for image caption formatter templates.
*
* Default template: image-caption-formatter.html.twig.
*
* @param array $variables
* An associative array containing:
*
* - item: An ImageItem object.
*
* - item_attributes: An optional associative array of html attributes to be
* placed in the img tag.
*
* - image_style: An optional image style.
*
* - url: An optional \Drupal\Core\Url object.
*
* - link_attributes: An array of attributes to add to the link if 'url' is
* set.
*
* - caption: An optional caption text.
*
* @see \Drupal\ambientimpact_media\Plugin\AmbientImpact\Component\AnimatedGIFToggle::preprocessImageFormatter()
* Variables are passed to this for modification.
*
* @see \Drupal\ambientimpact_media\Plugin\AmbientImpact\Component\RemoteVideo::preprocessImageFormatter()
* Variables are passed to this for modification.
*/
function ambientimpact_media_preprocess_image_caption_formatter(&$variables) {
// Pass the constrain_width variable on to the image element. This must be
// placed before we pass anything to the components below or the variable may
// not make it through all the way to the image template.
$variables['image']['#constrain_width'] = $variables['constrain_width'];
/** @var \Drupal\ambientimpact_coreComponentPluginManagerInterface The component manager service. */
$componentManager =
\Drupal::service('plugin.manager.ambientimpact_component');
$componentManager->getComponentInstance('animated_gif_toggle')
->preprocessImageFormatter($variables);
$componentManager->getComponentInstance('remote_video')
->preprocessImageFormatter($variables);
}
/**
* Prepares variables for image style templates.
*
* Default template: image-style.html.twig.
*
* This copies the 'constrain_width' variable over to $variables['image'] so
* that the value is inherited to the generated image.
*
* @param array $variables
* An associative array containing:
*
* - width: The width of the image.
*
* - height: The height of the image.
*
* - style_name: The name of the image style to be applied.
*
* - uri: URI of the source image before styling.
*
* - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
* always require an alt attribute. The HTML 5 draft allows the alt
* attribute to be omitted in some cases. Therefore, this variable defaults
* to an empty string, but can be set to NULL for the attribute to be
* omitted. Usually, neither omission nor an empty string satisfies
* accessibility requirements, so it is strongly encouraged for code using
* '#theme' => 'image_style' to pass a meaningful value for this variable.
* - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
* - http://www.w3.org/TR/xhtml1/dtds.html
* - http://dev.w3.org/html5/spec/Overview.html#alt
*
* - title: The title text is displayed when the image is hovered in some
* popular browsers.
*
* - attributes: Associative array of additional attributes to be placed in
* the img tag.
*
* - constrain_width: if true, templates should set a max-width equal to the
* natural width of the generated image to prevent phantom clickable areas
* if this image is in a link and less wide than the link itself.
*/
function ambientimpact_media_preprocess_image_style(&$variables) {
foreach (['constrain_width'] as $variableName) {
if (isset($variables[$variableName])) {
$variables['image']['#' . $variableName] = $variables[$variableName];
}
}
}