View source
<?php
namespace Drupal\gridstack\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyManagerInterface;
use Drupal\blazy\Dejavu\BlazyStylePluginBase;
use Drupal\gridstack\GridStackDefault;
use Drupal\gridstack\Entity\GridStack;
use Drupal\gridstack\GridStackManagerInterface;
class GridStackViews extends BlazyStylePluginBase {
protected $manager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, BlazyManagerInterface $blazy_manager, GridStackManagerInterface $manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $blazy_manager);
$this->manager = $manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('blazy.manager'), $container
->get('gridstack.manager'));
}
public function admin() {
return \Drupal::service('gridstack.admin');
}
public function manager() {
return $this->manager;
}
protected function defineOptions() {
$options = [
'stamp' => [
'default' => [],
],
];
foreach (GridStackDefault::extendedSettings() as $key => $value) {
$options[$key] = [
'default' => $value,
];
}
return $options + parent::defineOptions();
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$fields = [
'captions',
'layouts',
'images',
'links',
'titles',
'classes',
];
$definition = $this
->getDefinedFieldOptions($fields);
$this
->admin()
->buildSettingsForm($form, $definition);
$count = count($definition['captions']);
$wide = $count > 2 ? ' form--wide form--caption-' . $count : ' form--caption-' . $count;
$title = '<p class="form__header form__title">';
$title .= $this
->t('Check Vanilla if using content/custom markups, not fields. <small>See it under <strong>Format > Show</strong> section. Otherwise Gridstack markups apply which require some fields added below.</small>');
$title .= '</p>';
$form['opening']['#markup'] = '<div class="form--gridstack form--slick form--views form--half form--vanilla has-tooltip' . $wide . '">' . $title;
$form['image']['#description'] .= ' ' . $this
->t('Be sure to UNCHECK "Use field template" (by default already UNCHECKED) to have it work for Blazy lazyloading. Use Blazy formatters for relevant features such as Colorbox/Photobox/Photoswipe, or multimedia supports.');
}
public function render() {
$blazy = $this
->blazyManager();
$view = $this->view;
$settings = $this->options + GridStackDefault::entitySettings();
$view_name = $view->storage
->id();
$view_mode = $view->current_display;
$count = count($view->result);
$id = Blazy::getHtmlId("gridstack-{$view_name}-{$view_mode}", $settings['id']);
$optionset = GridStack::load($settings['optionset']);
$grids = $optionset
->getEndBreakpointGrids();
$settings += [
'cache_metadata' => [
'keys' => [
$id,
$view_mode,
$settings['optionset'],
],
],
'count' => $count,
'current_view_mode' => $view_mode,
'view_name' => $view_name,
];
$settings['id'] = $id;
$settings['item_id'] = 'box';
$settings['caption'] = array_filter($settings['caption']);
$settings['namespace'] = 'gridstack';
$settings['ratio'] = '';
$settings['_views'] = TRUE;
$optionset
->gridsJsonToArray($settings);
$elements = [];
foreach ($this
->renderGrouping($view->result, $settings['grouping']) as $rows) {
$settings = array_filter($settings);
$items = $this
->buildElements($settings, $rows, $grids);
$blazy
->isBlazy($settings, $items[0]);
$build = [
'items' => $items,
'optionset' => $optionset,
'settings' => $settings,
];
$elements = $this->manager
->build($build);
unset($build);
}
return $elements;
}
public function buildElements(array $settings, $rows, $grids = []) {
$build = [];
$view = $this->view;
$item_id = $settings['item_id'];
foreach ($rows as $delta => $row) {
$view->row_index = $delta;
$box = [];
$box['delta'] = $delta;
$box[$item_id] = [];
if (!empty($settings['breakpoints'])) {
foreach ($settings['breakpoints'] as $key => &$breakpoint) {
if (isset($breakpoint['image_style']) && !empty($breakpoint['grids'][$delta]) && !empty($breakpoint['grids'][$delta]['image_style'])) {
$breakpoint['image_style'] = $breakpoint['grids'][$delta]['image_style'];
}
if ($key == 'lg' && !empty($breakpoint['grids'][$delta]['image_style'])) {
$settings['_dimensions'] = empty($settings['background']);
$settings['image_style'] = $breakpoint['grids'][$delta]['image_style'];
}
}
}
$box['settings'] = $settings;
if (!empty($settings['class'])) {
$classes = $this
->getFieldString($row, $settings['class'], $delta);
$box['settings']['class'] = empty($classes[$delta]) ? [] : $classes[$delta];
}
if (!empty($settings['vanilla'])) {
$box[$item_id] = $view->rowPlugin
->render($row);
}
else {
$this
->buildElement($box, $row, $delta);
}
$build[] = $box;
unset($box);
}
unset($view->row_index);
return $build;
}
}