Updated all the contrib modules to their latest versions.
[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\Component\Utility\NestedArray;
11 use Drupal\blazy\Blazy;
12
13 /**
14  * Implements hook_theme().
15  */
16 function blazy_theme() {
17   return ['blazy' => ['render element' => 'element']];
18 }
19
20 /**
21  * Prepares variables for blazy.html.twig templates.
22  */
23 function template_preprocess_blazy(&$variables) {
24   Blazy::buildAttributes($variables);
25 }
26
27 /**
28  * Overrides variables for responsive-image.html.twig templates.
29  */
30 function blazy_preprocess_responsive_image(&$variables) {
31   $config = Blazy::getConfig();
32
33   // Do not proceed if disabled globally, or not a Blazy formatter.
34   if (!$config['responsive_image'] || !isset($variables['attributes']['data-srcset'])) {
35     return;
36   }
37
38   // We are here either using Blazy, or core Responsive image formatters.
39   Blazy::preprocessResponsiveImage($variables);
40 }
41
42 /**
43  * Implements hook_preprocess_field().
44  */
45 function blazy_preprocess_field(array &$variables) {
46   $element = $variables['element'];
47
48   // Only proceed if an image field and using Blazy formatter.
49   if (!isset($element['#blazy'])) {
50     return;
51   }
52
53   // Defines [data-blazy] attribute as required by the Blazy loader.
54   $settings = $element['#blazy'];
55   $variables['attributes']['class'][] = 'blazy';
56   $variables['attributes']['data-blazy'] = empty($settings['blazy_data']) ? '' : Json::encode($settings['blazy_data']);
57   if (!empty($settings['media_switch'])) {
58     $switch = str_replace('_', '-', $settings['media_switch']);
59     $variables['attributes']['data-' . $switch . '-gallery'] = TRUE;
60   }
61 }
62
63 /**
64  * Implements hook_views_pre_render().
65  */
66 function blazy_views_pre_render($view) {
67   if (!isset($view)) {
68     return;
69   }
70   \Drupal\blazy\BlazyViews::viewsPreRender($view);
71 }
72
73 /**
74  * Implements hook_field_formatter_info_alter().
75  */
76 function blazy_field_formatter_info_alter(array &$info) {
77   // Supports optional Media Entity via VEM/VEF if available.
78   $common = [
79     'description' => t('Displays lazyloaded images, or iframes, for VEF/ ME.'),
80     'quickedit'   => ['editor' => 'disabled'],
81     'provider'    => 'blazy',
82   ];
83   if (\Drupal::moduleHandler()->moduleExists('video_embed_media')) {
84     $info['blazy_file'] = $common + [
85       'id'          => 'blazy_file',
86       'label'       => t('Blazy Image with Media'),
87       'class'       => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter',
88       'field_types' => ['entity_reference', 'image'],
89     ];
90
91     $info['blazy_video'] = $common + [
92       'id'          => 'blazy_video',
93       'label'       => t('Blazy Video'),
94       'class'       => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyVideoFormatter',
95       'field_types' => ['video_embed_field'],
96     ];
97   }
98 }
99
100 /**
101  * Implements hook_config_schema_info_alter().
102  */
103 function blazy_config_schema_info_alter(array &$definitions) {
104   Blazy::configSchemaInfoAlter($definitions, 'blazy_base');
105 }
106
107 /**
108  * Implements hook_library_info_alter().
109  */
110 function blazy_library_info_alter(&$libraries, $extension) {
111   if ($extension === 'blazy' && function_exists('libraries_get_path')) {
112     $libraries['blazy']['js'] = ['/' . libraries_get_path('blazy') . '/blazy.min.js' => ['weight' => -4]];
113   }
114 }
115
116 /**
117  * Implements hook_blazy_attach_alter().
118  */
119 function blazy_blazy_attach_alter(array &$load, $attach = []) {
120   if (!empty($attach['colorbox'])) {
121     $dummy = [];
122     \Drupal::service('colorbox.attachment')->attach($dummy);
123     $load = isset($dummy['#attached']) ? NestedArray::mergeDeep($load, $dummy['#attached']) : $load;
124     $load['library'][] = 'blazy/colorbox';
125     unset($dummy);
126   }
127 }
128
129 /**
130  * Implements hook_blazy_lightboxes_alter().
131  */
132 function blazy_blazy_lightboxes_alter(array &$lightboxes) {
133   if (is_file(DRUPAL_ROOT . '/libraries/photobox/photobox/jquery.photobox.js')) {
134     $lightboxes[] = 'photobox';
135   }
136 }
137
138 /**
139  * Implements hook_blazy_settings_alter().
140  */
141 function blazy_blazy_settings_alter(array &$build, $items) {
142   $settings = &$build['settings'];
143
144   // Sniffs for Views to allow block__no_wrapper, views_no_wrapper, etc.
145   if (function_exists('views_get_current_view') && $view = views_get_current_view()) {
146     $settings['view_name'] = $view->storage->id();
147     $settings['current_view_mode'] = $view->current_display;
148   }
149 }
150
151 /**
152  * Implements hook_help().
153  */
154 function blazy_help($route_name, RouteMatchInterface $route_match) {
155   switch ($route_name) {
156     case 'help.page.blazy':
157       return check_markup(file_get_contents(dirname(__FILE__) . '/README.txt'));
158   }
159 }