Pathologic was missing because of a .git folder inside.
[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
84   if (function_exists('video_embed_media_media_bundle_insert')) {
85     $info['blazy_file'] = $common + [
86       'id'          => 'blazy_file',
87       'label'       => t('Blazy Image with Media'),
88       'class'       => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter',
89       'field_types' => ['entity_reference', 'image'],
90     ];
91
92     $info['blazy_video'] = $common + [
93       'id'          => 'blazy_video',
94       'label'       => t('Blazy Video'),
95       'class'       => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyVideoFormatter',
96       'field_types' => ['video_embed_field'],
97     ];
98   }
99 }
100
101 /**
102  * Implements hook_config_schema_info_alter().
103  */
104 function blazy_config_schema_info_alter(array &$definitions) {
105   Blazy::configSchemaInfoAlter($definitions, 'blazy_base');
106 }
107
108 /**
109  * Implements hook_library_info_alter().
110  */
111 function blazy_library_info_alter(&$libraries, $extension) {
112   if ($extension === 'blazy' && function_exists('libraries_get_path')) {
113     $libraries['blazy']['js'] = ['/' . libraries_get_path('blazy') . '/blazy.min.js' => ['weight' => -4]];
114   }
115 }
116
117 /**
118  * Implements hook_blazy_attach_alter().
119  */
120 function blazy_blazy_attach_alter(array &$load, $attach = []) {
121   if (!empty($attach['colorbox'])) {
122     $dummy = [];
123     \Drupal::service('colorbox.attachment')->attach($dummy);
124     $load = isset($dummy['#attached']) ? NestedArray::mergeDeep($load, $dummy['#attached']) : $load;
125     $load['library'][] = 'blazy/colorbox';
126     unset($dummy);
127   }
128 }
129
130 /**
131  * Implements hook_blazy_lightboxes_alter().
132  */
133 function blazy_blazy_lightboxes_alter(array &$lightboxes) {
134   if (is_file(DRUPAL_ROOT . '/libraries/photobox/photobox/jquery.photobox.js')) {
135     $lightboxes[] = 'photobox';
136   }
137 }
138
139 /**
140  * Implements hook_blazy_settings_alter().
141  */
142 function blazy_blazy_settings_alter(array &$build, $items) {
143   $settings = &$build['settings'];
144
145   // Sniffs for Views to allow block__no_wrapper, views_no_wrapper, etc.
146   if (function_exists('views_get_current_view') && $view = views_get_current_view()) {
147     $settings['view_name'] = $view->storage->id();
148     $settings['current_view_mode'] = $view->current_display;
149   }
150 }
151
152 /**
153  * Implements hook_help().
154  */
155 function blazy_help($route_name, RouteMatchInterface $route_match) {
156   switch ($route_name) {
157     case 'help.page.blazy':
158       return check_markup(file_get_contents(dirname(__FILE__) . '/README.txt'));
159   }
160 }