View source
<?php
namespace Drupal\blazy;
use Drupal\Component\Utility\NestedArray;
class BlazyViews {
public static function viewsPreRender($view) {
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;
}
if (!$grid) {
$view->element['#attributes'] = empty($view->element['#attributes']) ? [] : $view->element['#attributes'];
Blazy::containerAttributes($view->element['#attributes'], $settings);
}
}
}
public static function viewsField($view) {
foreach ([
'file',
'media',
] as $entity) {
if (isset($view->field['blazy_' . $entity])) {
return $view->field['blazy_' . $entity];
}
}
return FALSE;
}
public static function preprocessViewsView(array &$variables, $lightboxes) {
preg_match('~blazy--(.*?)-gallery~', $variables['css_class'], $matches);
$lightbox = $matches[1] ? str_replace('-', '_', $matches[1]) : FALSE;
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);
}
}
}