View source
<?php
namespace Drupal\lightgallery\Plugin\views\style;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;
use Drupal\lightgallery\Manager\LightgalleryManager;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LightGallery extends StylePluginBase {
protected $usesRowPlugin = FALSE;
protected $usesFields = TRUE;
protected $usesOptions = TRUE;
protected $usesGrouping = FALSE;
protected $usesRowClass = FALSE;
protected $entityFieldManager;
protected $fieldSources;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_field.manager'));
}
public function evenEmpty() {
return FALSE;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$this->fieldSources = $this
->confGetFieldSources();
$field_images = $this
->getImageFields();
$field_others = $this
->getNonImageFields();
$missing_field_warning = '';
if (empty($field_images)) {
$missing_field_warning = $this
->t('<strong>You must add a field of type image to your view display before this value can be set.</strong><br/>');
}
$fields_settings = LightgalleryManager::getSettingFields();
foreach ($fields_settings as $field) {
$group = $field
->getGroup();
if (empty($form[$group
->getName()])) {
$form[$group
->getName()] = [
'#type' => 'details',
'#title' => $group
->getTitle(),
'#open' => !empty($group
->getOpenValue()) ? $this->options['lightgallery'][$group
->getOpenValue()] : $group
->isOpen(),
];
}
if ($field
->appliesToViews()) {
$form[$group
->getName()][$field
->getName()] = [
'#type' => $field
->getType(),
'#title' => $field
->getTitle(),
'#default_value' => isset($this->options['lightgallery'][$field
->getName()]) ? $this->options['lightgallery'][$field
->getName()] : $field
->getDefaultValue(),
'#description' => $field
->getDescription(),
'#required' => $field
->isRequired(),
];
if ($field
->getName() == 'thumb_field' || $field
->getName() == 'image_field') {
$form[$group
->getName()][$field
->getName()]['#suffix'] = $missing_field_warning;
}
if (!empty($field
->getOptions())) {
if (is_callable($field
->getOptions())) {
$form[$group
->getName()][$field
->getName()]['#options'] = call_user_func($field
->getOptions());
}
}
}
}
}
public function validateOptionsForm(array &$form, FormStateInterface $form_state) {
parent::validateOptionsForm($form, $form_state);
$style_options = $form_state
->getValue('style_options');
$form_state
->setValue([
'style_options',
'lightgallery',
], LightgalleryManager::flattenArray($style_options));
$form_state
->unsetValue([
'style_options',
'lightgallery_core',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_thumbs',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_autoplay',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_full_screen',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_pager',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_zoom',
]);
$form_state
->unsetValue([
'style_options',
'lightgallery_hash',
]);
}
protected function confGetFieldSources() {
$options = [
'field_options_images' => [],
'field_options' => [],
];
$view = $this->view;
$field_handlers = $view->display_handler
->getHandlers('field');
$field_labels = $view->display_handler
->getFieldLabels();
foreach ($field_handlers as $field => $handler) {
$is_image = FALSE;
$id = $handler
->getPluginId();
$name = $field_labels[$field];
if ($id == 'field') {
$entity_type = $handler
->getEntityType();
$field_name = $handler->field;
$field_definition = $this->entityFieldManager
->getFieldStorageDefinitions($entity_type)[$field_name];
if ($field_definition) {
$field_type = $field_definition
->getType();
if ($field_type == 'image' || $field_type == 'file') {
$field_cardinality = $field_definition
->get('cardinality');
$options['field_options_images'][$field] = $name . ($field_cardinality == 1 ? '' : '*');
$is_image = TRUE;
}
}
}
if (!$is_image) {
$options['field_options'][$field] = $name;
}
}
return $options;
}
public function renderFields(array $result) {
$rendered_fields = [];
$this->view->row_index = 0;
$keys = array_keys($this->view->field);
if (!empty($keys)) {
$fields = $this->view->field;
$field_sources = $this
->confGetFieldSources();
$image_fields = array_keys($field_sources['field_options_images']);
foreach ($result as $count => $row) {
$this->view->row_index = $count;
foreach ($keys as $id) {
if (in_array($id, $image_fields)) {
$image_style = $this->view->field[$id]->options['settings']['image_style'];
$field_name = $fields[$id]->field;
$file = $result[$count]->_entity->{$field_name}->entity;
if ($file instanceof FileInterface && ($uri = $file
->getFileUri())) {
if (!empty($image_style)) {
$rendered_fields[$count][$id] = ImageStyle::load($image_style)
->buildUrl($uri);
}
else {
$rendered_fields[$count][$id] = file_create_url($uri);
}
}
}
else {
$rendered_fields[$count][$id] = $this->view->field[$id]
->render($row);
}
}
$this->rowTokens[$count] = $this->view->field[$id]
->getRenderTokens([]);
}
}
unset($this->view->row_index);
return $rendered_fields;
}
private function getImageFields() {
return !empty($this->fieldSources['field_options_images']) ? $this->fieldSources['field_options_images'] : [];
}
private function getNonImageFields() {
return !empty($this->fieldSources['field_options']) ? $this->fieldSources['field_options'] : [];
}
}