3 namespace Drupal\blazy;
5 use Drupal\Component\Utility\NestedArray;
8 * Provides optional Views integration.
13 * Implements hook_views_pre_render().
15 public static function viewsPreRender($view) {
16 // Load Blazy library once, not per field, if any Blazy Views field found.
17 if ($blazy = self::viewsField($view)) {
18 $plugin_id = $view->getStyle()->getPluginId();
19 $settings = $blazy->mergedViewsSettings();
20 $load = $blazy->blazyManager()->attach($settings);
22 // Enforce Blazy to work with hidden element such as with EB selection.
23 $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
24 $view->element['#attached'] = isset($view->element['#attached']) ? NestedArray::mergeDeep($view->element['#attached'], $load) : $load;
26 $grid = $plugin_id == 'blazy';
27 if ($options = $view->getStyle()->options) {
28 $grid = empty($options['grid']) ? $grid : TRUE;
31 // Prevents dup [data-LIGHTBOX-gallery] if the Views style supports Grid.
33 $view->element['#attributes']['class'][] = 'blazy';
34 $view->element['#attributes']['data-blazy'] = TRUE;
35 if (!empty($settings['media_switch'])) {
36 $switch = str_replace('_', '-', $settings['media_switch']);
37 $view->element['#attributes']['data-' . $switch . '-gallery'] = TRUE;
44 * Returns one of the Blazy Views fields, if available.
46 public static function viewsField($view) {
47 foreach (['file', 'media'] as $entity) {
48 if (isset($view->field['blazy_' . $entity])) {
49 return $view->field['blazy_' . $entity];