View source
<?php
namespace Drupal\gridstack\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
use Drupal\blazy\Dejavu\BlazyStylePluginBase;
use Drupal\gridstack\GridStackDefault;
use Drupal\gridstack\Entity\GridStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GridStackViews extends BlazyStylePluginBase {
protected $manager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->manager = $container
->get('gridstack.manager');
$instance->blazyManager = isset($instance->blazyManager) ? $instance->blazyManager : $container
->get('blazy.manager');
return $instance;
}
public function manager() {
return $this->manager;
}
public function admin() {
return \Drupal::service('gridstack.admin');
}
protected function defineOptions() {
$options = [
'stamp' => [
'default' => '',
],
];
foreach (GridStackDefault::extendedSettings() as $key => $value) {
$options[$key] = [
'default' => $value,
];
}
return $options + parent::defineOptions();
}
protected function getDefinedFormScopes(array $extra_fields = []) {
$fields = GridStackDefault::viewFieldOptions();
$fields = array_merge($fields, $extra_fields);
$definition = $this
->getDefinedFieldOptions($fields);
$definition['opening_class'] = 'form--views';
$definition['_views'] = TRUE;
return $definition;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$definition = $this
->getDefinedFormScopes();
$this
->buildSettingsForm($form, $definition);
}
protected function buildSettingsForm(&$form, &$definition) {
$count = empty($definition['captions']) ? 0 : count($definition['captions']);
$definition['captions_count'] = $count;
$definition['stamps'] = $this
->getViewsAsOptions('html_list');
$this
->admin()
->buildSettingsForm($form, $definition);
$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'] .= $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, etc., or multimedia supports.');
}
protected function buildSettings() {
$this->options['item_id'] = 'box';
$this->options['namespace'] = 'gridstack';
$settings = parent::buildSettings();
$settings['caption'] = array_filter($settings['caption']);
$settings['ratio'] = '';
$settings['_noratio'] = TRUE;
return $settings;
}
public function render() {
$settings = $this
->buildSettings();
$optionset = GridStack::loadWithFallback($settings['optionset']);
$elements = [];
foreach ($this
->renderGrouping($this->view->result, $settings['grouping']) as $rows) {
$settings = array_filter($settings, function ($value) {
return $value !== NULL && $value !== '' && $value !== [];
});
$items = $this
->buildElements($settings, $rows);
$settings['first_image'] = isset($rows[0]) ? $this
->getFirstImage($rows[0]) : [];
$build = [
'items' => $items,
'optionset' => $optionset,
'settings' => $settings,
];
$elements = $this->manager
->build($build);
unset($build);
}
return $elements;
}
public function buildElements(array $settings, $rows) {
$build = [];
$item_id = $settings['item_id'];
$stamp_index = isset($settings['stamp_index']) ? $settings['stamp_index'] : -1;
foreach ($rows as $delta => $row) {
$this->view->row_index = $delta;
$box = $box['attributes'] = [];
$settings['delta'] = $delta;
if (!empty($settings['image']) && $this
->getField($delta, $settings['image'])) {
$settings['type'] = $row->_entity
->hasField($settings['image']) ? $row->_entity
->getFieldDefinition($settings['image'])
->getFieldStorageDefinition()
->getType() : '';
}
$box['settings'] = $settings;
if (!empty($settings['vanilla'])) {
$box[$item_id] = $this->view->rowPlugin
->render($row);
}
else {
$this
->buildElement($box, $row, $delta);
}
$this
->buildElementExtra($box, $row, $delta);
$stamp = [];
if ($delta == $stamp_index && !empty($box['stamp'])) {
$stamp = $box['stamp'];
$stamp[$stamp_index]['settings']['type'] = 'stamp';
unset($box['stamp']);
}
$build[] = $box;
if ($delta == $stamp_index && !empty($stamp)) {
array_splice($build, $stamp_index, 0, $stamp);
}
unset($box);
}
unset($this->view->row_index);
return $build;
}
public function buildElement(array &$box, $row, $delta) {
$settings =& $box['settings'];
$item_id = $settings['item_id'];
$use_category = TRUE;
if (!empty($settings['class'])) {
$classes = $this
->getFieldString($row, $settings['class'], $delta);
if (!empty($classes[$delta])) {
$box['attributes']['class'][] = $classes[$delta];
}
}
if (!empty($settings['overlay']) && $this
->getField($delta, $settings['overlay'])) {
$box[$item_id] = $this
->getFieldRendered($delta, $settings['overlay']);
$settings['type'] = 'rich';
$use_category = FALSE;
foreach ([
'caption',
'image',
'link',
'overlay',
'title',
] as $key) {
$settings[$key] = '';
}
}
$stamp_index = isset($settings['stamp_index']) ? $settings['stamp_index'] : -1;
if ($delta == $stamp_index && !empty($settings['stamp'])) {
list($view_id, $display_id) = explode(":", $settings['stamp'], 2);
$box['stamp'][$stamp_index] = [
$item_id => views_embed_view($view_id, $display_id),
'settings' => $settings,
'attributes' => isset($box['attributes']) ? $box['attributes'] : [],
];
}
parent::buildElement($box, $row, $delta);
if ($use_category && !empty($settings['category']) && $this
->getField($delta, $settings['category'])) {
$box['caption']['category'] = $this
->getFieldRendered($delta, $settings['category']);
}
}
public function buildElementExtra(array &$box, $row, $delta) {
}
}