Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / blazy / src / BlazyViews.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 use Drupal\Component\Utility\NestedArray;
6
7 /**
8  * Provides optional Views integration.
9  */
10 class BlazyViews {
11
12   /**
13    * Implements hook_views_pre_render().
14    */
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);
21
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;
25
26       $grid = $plugin_id == 'blazy';
27       if ($options = $view->getStyle()->options) {
28         $grid = empty($options['grid']) ? $grid : TRUE;
29       }
30
31       // Prevents dup [data-LIGHTBOX-gallery] if the Views style supports Grid.
32       if (!$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;
38         }
39       }
40     }
41   }
42
43   /**
44    * Returns one of the Blazy Views fields, if available.
45    */
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];
50       }
51     }
52     return FALSE;
53   }
54
55 }