You are here

class BlazyViews in Blazy 8.2

Same name in this branch
  1. 8.2 src/BlazyViews.php \Drupal\blazy\BlazyViews
  2. 8.2 src/Plugin/views/style/BlazyViews.php \Drupal\blazy\Plugin\views\style\BlazyViews
Same name and namespace in other branches
  1. 8 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);
      $view->element['#attached'] = empty($view->element['#attached']) ? $load : NestedArray::mergeDeep($view->element['#attached'], $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) {
        $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;
  }

  /**
   * Implements hook_preprocess_views_view().
   */
  public static function preprocessViewsView(array &$variables, $lightboxes) {
    preg_match('~blazy--(.*?)-gallery~', $variables['css_class'], $matches);
    $lightbox = $matches[1] ? str_replace('-', '_', $matches[1]) : FALSE;

    // Given blazy--photoswipe-gallery, adds the [data-photoswipe-gallery], etc.
    if ($lightbox && in_array($lightbox, $lightboxes)) {
      $settings['namespace'] = 'blazy';
      $settings['media_switch'] = $matches[1];
      $variables['attributes'] = empty($variables['attributes']) ? [] : $variables['attributes'];
      Blazy::containerAttributes($variables['attributes'], $settings);
    }
  }

}

Members

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