5 * Provides basic Blazy integration for lazy loading and multi-serving images.
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Component\Serialization\Json;
10 use Drupal\blazy\Blazy;
13 * Implements hook_theme().
15 function blazy_theme() {
16 return ['blazy' => ['render element' => 'element']];
20 * Prepares variables for blazy.html.twig templates.
22 function template_preprocess_blazy(&$variables) {
23 Blazy::buildAttributes($variables);
27 * Overrides variables for responsive-image.html.twig templates.
29 function blazy_preprocess_responsive_image(&$variables) {
30 $config = Blazy::getConfig();
32 // Do not proceed if disabled globally, or not a Blazy formatter.
33 if (!$config['responsive_image'] || !isset($variables['attributes']['data-srcset'])) {
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);
43 // Fetches the picture element fallback URI, and empty it later.
44 $fallback_uri = $variables['img_element']['#srcset'][0]['uri'];
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;
53 $srcset = $variables['attributes']['srcset'];
54 $srcset_values = $srcset->value();
55 $fallback_uri = $variables['img_element']['#uri'];
57 $variables['attributes']['data-srcset'] = $srcset_values;
58 $variables['img_element']['#attributes']['data-srcset'] = $srcset_values;
59 $variables['img_element']['#attributes']['srcset'] = '';
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';
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;
72 $variables['img_element']['#attached']['drupalSettings']['blazy'] = $config['blazy'];
76 * Adds [data-srcset] attribute to picture source element to support lazyload.
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();
84 $source->setAttribute('data-srcset', $srcset_values);
85 $source->removeAttribute('srcset');
90 * Implements hook_preprocess_field().
92 function blazy_preprocess_field(array &$variables) {
93 $element = $variables['element'];
95 // Only proceed if an image field and using Blazy formatter.
96 if (!isset($element['#blazy'])) {
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;
111 * Implements hook_views_pre_render().
113 function blazy_views_pre_render($view) {
117 \Drupal\blazy\BlazyViews::viewsPreRender($view);
121 * Implements hook_field_formatter_info_alter().
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',
139 * Implements hook_config_schema_info_alter().
141 function blazy_config_schema_info_alter(array &$definitions) {
142 Blazy::configSchemaInfoAlter($definitions, 'blazy_base');
146 * Implements hook_library_info_alter().
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' => []];
155 * Implements hook_help().
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'));