You are here

class BlazyViews in Blazy 8

Same name in this branch
  1. 8 src/BlazyViews.php \Drupal\blazy\BlazyViews
  2. 8 src/Plugin/views/style/BlazyViews.php \Drupal\blazy\Plugin\views\style\BlazyViews
Same name and namespace in other branches
  1. 8.2 src/BlazyViews.php \Drupal\blazy\BlazyViews

Provides optional Views integration.

Hierarchy

Expanded class hierarchy of BlazyViews

2 files declare their use of BlazyViews
blazy.module in ./blazy.module
Provides basic Blazy integration for lazy loading and multi-serving images.
BlazyViewsFileTest.php in tests/src/Kernel/Views/BlazyViewsFileTest.php

File

src/BlazyViews.php, line 10

Namespace

Drupal\blazy
View source
class BlazyViews {

  /**
   * Implements hook_views_pre_render().
   */
  public static function viewsPreRender($view) {

    // Load Blazy library once, not per field, if any Blazy Views field found.
    if ($blazy = self::viewsField($view)) {
      $plugin_id = $view
        ->getStyle()
        ->getPluginId();
      $settings = $blazy
        ->mergedViewsSettings();
      $load = $blazy
        ->blazyManager()
        ->attach($settings);

      // Enforce Blazy to work with hidden element such as with EB selection.
      $load['drupalSettings']['blazy']['loadInvisible'] = TRUE;
      $view->element['#attached'] = isset($view->element['#attached']) ? NestedArray::mergeDeep($view->element['#attached'], $load) : $load;
      $grid = $plugin_id == 'blazy';
      if ($options = $view
        ->getStyle()->options) {
        $grid = empty($options['grid']) ? $grid : TRUE;
      }

      // Prevents dup [data-LIGHTBOX-gallery] if the Views style supports Grid.
      if (!$grid) {

        // @todo remove conditions when confident, kept to avoid the unexpected.
        $view->element['#attributes'] = empty($view->element['#attributes']) ? [] : $view->element['#attributes'];
        Blazy::containerAttributes($view->element['#attributes'], $settings);
      }
    }
  }

  /**
   * Returns one of the Blazy Views fields, if available.
   */
  public static function viewsField($view) {
    foreach ([
      'file',
      'media',
    ] as $entity) {
      if (isset($view->field['blazy_' . $entity])) {
        return $view->field['blazy_' . $entity];
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyViews::viewsField public static function Returns one of the Blazy Views fields, if available.
BlazyViews::viewsPreRender public static function Implements hook_views_pre_render().