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\Component\Utility\NestedArray;
11 use Drupal\blazy\Blazy;
14 * Implements hook_theme().
16 function blazy_theme() {
17 return ['blazy' => ['render element' => 'element']];
21 * Prepares variables for blazy.html.twig templates.
23 function template_preprocess_blazy(&$variables) {
24 Blazy::buildAttributes($variables);
28 * Overrides variables for responsive-image.html.twig templates.
30 function blazy_preprocess_responsive_image(&$variables) {
31 $config = Blazy::getConfig();
33 // Do not proceed if disabled globally, or not a Blazy formatter.
34 if (!$config['responsive_image'] || !isset($variables['attributes']['data-srcset'])) {
38 // We are here either using Blazy, or core Responsive image formatters.
39 Blazy::preprocessResponsiveImage($variables);
43 * Implements hook_preprocess_field().
45 function blazy_preprocess_field(array &$variables) {
46 $element = $variables['element'];
48 // Only proceed if an image field and using Blazy formatter.
49 if (!isset($element['#blazy'])) {
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;
64 * Implements hook_views_pre_render().
66 function blazy_views_pre_render($view) {
70 \Drupal\blazy\BlazyViews::viewsPreRender($view);
74 * Implements hook_field_formatter_info_alter().
76 function blazy_field_formatter_info_alter(array &$info) {
77 // Supports optional Media Entity via VEM/VEF if available.
79 'description' => t('Displays lazyloaded images, or iframes, for VEF/ ME.'),
80 'quickedit' => ['editor' => 'disabled'],
81 'provider' => 'blazy',
83 if (\Drupal::moduleHandler()->moduleExists('video_embed_media')) {
84 $info['blazy_file'] = $common + [
86 'label' => t('Blazy Image with Media'),
87 'class' => 'Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter',
88 'field_types' => ['entity_reference', 'image'],
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'],
101 * Implements hook_config_schema_info_alter().
103 function blazy_config_schema_info_alter(array &$definitions) {
104 Blazy::configSchemaInfoAlter($definitions, 'blazy_base');
108 * Implements hook_library_info_alter().
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]];
117 * Implements hook_blazy_attach_alter().
119 function blazy_blazy_attach_alter(array &$load, $attach = []) {
120 if (!empty($attach['colorbox'])) {
122 \Drupal::service('colorbox.attachment')->attach($dummy);
123 $load = isset($dummy['#attached']) ? NestedArray::mergeDeep($load, $dummy['#attached']) : $load;
124 $load['library'][] = 'blazy/colorbox';
130 * Implements hook_blazy_lightboxes_alter().
132 function blazy_blazy_lightboxes_alter(array &$lightboxes) {
133 if (is_file(DRUPAL_ROOT . '/libraries/photobox/photobox/jquery.photobox.js')) {
134 $lightboxes[] = 'photobox';
139 * Implements hook_blazy_settings_alter().
141 function blazy_blazy_settings_alter(array &$build, $items) {
142 $settings = &$build['settings'];
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;
152 * Implements hook_help().
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'));