Version 1
[yaffs-website] / web / modules / contrib / blazy / blazy.module
1 <?php
2
3 /**
4  * @file
5  * Provides basic Blazy integration for lazy loading and multi-serving images.
6  */
7
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Component\Serialization\Json;
10 use Drupal\blazy\Blazy;
11
12 /**
13  * Implements hook_theme().
14  */
15 function blazy_theme() {
16   return ['blazy' => ['render element' => 'element']];
17 }
18
19 /**
20  * Prepares variables for blazy.html.twig templates.
21  */
22 function template_preprocess_blazy(&$variables) {
23   Blazy::buildAttributes($variables);
24 }
25
26 /**
27  * Overrides variables for responsive-image.html.twig templates.
28  */
29 function blazy_preprocess_responsive_image(&$variables) {
30   $config = Blazy::getConfig();
31
32   // Do not proceed if disabled globally, or not a Blazy formatter.
33   if (!$config['responsive_image'] || !isset($variables['attributes']['data-srcset'])) {
34     return;
35   }
36
37   // We are here either using Blazy, or core Responsive image formatters.
38   // Is picture element.
39   if (!$variables['output_image_tag']) {
40     // Prepare all <picture> [data-srcset] attributes on <source> elements.
41     _blazy_preprocess_responsive_image_picture_sources($variables);
42
43     // Fetches the picture element fallback URI, and empty it later.
44     $fallback_uri = $variables['img_element']['#srcset'][0]['uri'];
45
46     // Cleans up the no-longer relevant attributes for controlling element.
47     unset($variables['attributes']['data-srcset'], $variables['img_element']['#attributes']['data-srcset']);
48     $variables['img_element']['#srcset'] = '';
49     // Prevents invalid IMG tag when one pixel placeholder is disabled.
50     $variables['img_element']['#uri'] = Blazy::PLACEHOLDER;
51   }
52   else {
53     $srcset = $variables['attributes']['srcset'];
54     $srcset_values = $srcset->value();
55     $fallback_uri = $variables['img_element']['#uri'];
56
57     $variables['attributes']['data-srcset'] = $srcset_values;
58     $variables['img_element']['#attributes']['data-srcset'] = $srcset_values;
59     $variables['img_element']['#attributes']['srcset'] = '';
60   }
61
62   // Blazy needs controlling element to have a fallback [data-src], else error.
63   $variables['img_element']['#attributes']['data-src'] = $fallback_uri;
64   $variables['img_element']['#attributes']['class'][] = 'b-lazy b-responsive';
65
66   // Only replace fallback image URI with 1px placeholder, if so configured.
67   // This will prevent downloading the fallback image.
68   if ($config['one_pixel']) {
69     $variables['img_element']['#uri'] = Blazy::PLACEHOLDER;
70   }
71
72   $variables['img_element']['#attached']['drupalSettings']['blazy'] = $config['blazy'];
73 }
74
75 /**
76  * Adds [data-srcset] attribute to picture source element to support lazyload.
77  */
78 function _blazy_preprocess_responsive_image_picture_sources(&$variables) {
79   /** @var \Drupal\Core\Template\Attribute $source */
80   foreach ($variables['sources'] as &$source) {
81     $srcset = $source['srcset'];
82     $srcset_values = $srcset->value();
83
84     $source->setAttribute('data-srcset', $srcset_values);
85     $source->removeAttribute('srcset');
86   }
87 }
88
89 /**
90  * Implements hook_preprocess_field().
91  */
92 function blazy_preprocess_field(array &$variables) {
93   $element = $variables['element'];
94
95   // Only proceed if an image field and using Blazy formatter.
96   if (!isset($element['#blazy'])) {
97     return;
98   }
99
100   // Defines [data-blazy] attribute as required by the Blazy loader.
101   $settings = $element['#blazy'];
102   $variables['attributes']['class'][] = 'blazy';
103   $variables['attributes']['data-blazy'] = empty($settings['blazy_data']) ? '' : Json::encode($settings['blazy_data']);
104   if (!empty($settings['media_switch'])) {
105     $switch = str_replace('_', '-', $settings['media_switch']);
106     $variables['attributes']['data-' . $switch . '-gallery'] = TRUE;
107   }
108 }
109
110 /**
111  * Implements hook_views_pre_render().
112  */
113 function blazy_views_pre_render($view) {
114   if (!isset($view)) {
115     return;
116   }
117   \Drupal\blazy\BlazyViews::viewsPreRender($view);
118 }
119
120 /**
121  * Implements hook_field_formatter_info_alter().
122  */
123 function blazy_field_formatter_info_alter(array &$info) {
124   // Supports optional Media Entity via VEM within VEF if available.
125   if (function_exists('video_embed_media_media_bundle_insert')) {
126     $info['blazy_file'] = [
127       'id'          => 'blazy_file',
128       'label'       => t('Blazy Image with Media'),
129       'description' => t('Display the images associated to VEM/ME as videos.'),
130       'class'       => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter',
131       'field_types' => ['entity_reference', 'image'],
132       'quickedit'   => ['editor' => 'disabled'],
133       'provider'    => 'blazy',
134     ];
135   }
136 }
137
138 /**
139  * Implements hook_config_schema_info_alter().
140  */
141 function blazy_config_schema_info_alter(array &$definitions) {
142   Blazy::configSchemaInfoAlter($definitions, 'blazy_base');
143 }
144
145 /**
146  * Implements hook_library_info_alter().
147  */
148 function blazy_library_info_alter(&$libraries, $extension) {
149   if ($extension === 'blazy' && function_exists('libraries_get_path')) {
150     $libraries['blazy']['js'] = ['/' . libraries_get_path('blazy') . '/blazy.min.js' => []];
151   }
152 }
153
154 /**
155  * Implements hook_help().
156  */
157 function blazy_help($route_name, RouteMatchInterface $route_match) {
158   switch ($route_name) {
159     case 'help.page.blazy':
160       return check_markup(file_get_contents(dirname(__FILE__) . '/README.txt'));
161   }
162 }