View source
<?php
namespace Drupal\bootstrap_basic_image_gallery\Plugin\Field\FieldFormatter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\image\Entity\ImageStyle;
use Drupal\Component\Utility\Html;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Config\ConfigFactory;
class BootstrapBasicImageGalleryFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {
protected $imageStyleStorage;
protected $themeManager;
protected $configFactory;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $image_style_storage, ThemeManagerInterface $theme_manager, ConfigFactory $configFactory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->imageStyleStorage = $image_style_storage;
$this->themeManager = $theme_manager;
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('entity_type.manager')
->getStorage('image_style'), $container
->get('theme.manager'), $container
->get('config.factory'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$files = $this
->getEntitiesToView($items, $langcode);
if (empty($files)) {
return [];
}
$thumbnailsPerRow = $this
->getSetting('thumbnails_per_row');
$thumbnailWidth = 100;
if ($thumbnailsPerRow > 0) {
$thumbnailWidth = 100 / $thumbnailsPerRow;
}
$imageGallery = [
'#attached' => [
'library' => [
'bootstrap_basic_image_gallery/bootstrap_basic',
'bootstrap_basic_image_gallery/hover_preview',
],
],
'#theme' => 'bootstrap_basic_image_gallery',
'#main' => [],
'#thumbnails' => [
'class' => 'bscol-' . $thumbnailsPerRow,
'width' => $thumbnailWidth,
'images' => [],
],
'#lazyload' => $this
->getSetting('lazyload') ? 'lazy' : '',
'#modal' => [
'id' => Html::getUniqueId('bootstrap-basic-image-gallery-modal'),
'label' => $this->fieldDefinition
->getLabel(),
],
'#carousel' => [
'id' => Html::getUniqueId('bootstrap-basic-image-gallery-carousel'),
'autoplay' => $this
->getSetting('carousel_autorotate') ? 'carousel' : 'false',
'interval' => $this
->getSetting('carousel_interval'),
'images' => [],
],
];
$configuration_settings = $this->configFactory
->get('bootstrap_basic_image_gallery.settings');
if (!$configuration_settings
->get('prevent_load_bootstrap')) {
$bootstrap_included = FALSE;
foreach ($this->themeManager
->getActiveTheme()
->getLibraries() as $library) {
if ($bootstrap_included = preg_match('%^bootstrap%', $library)) {
break;
}
}
if (!$bootstrap_included) {
$imageGallery['#attached']['library'][] = 'bootstrap_basic_image_gallery/bootstrap_components';
}
}
if ($this
->getSetting('lazyload')) {
$imageGallery['#attached']['library'][] = 'bootstrap_basic_image_gallery/lazyload';
}
$thumbnail_cache_tags = [];
$thumbnail_image_style = $this
->getSetting('thumbnail_image_style');
if (!empty($thumbnail_image_style)) {
$image_style = $this->imageStyleStorage
->load($thumbnail_image_style);
$thumbnail_cache_tags = $image_style
->getCacheTags();
}
$main_image_style_setting = $this
->getSetting('image_style');
if (!empty($main_image_style_setting)) {
$main_image_style = $this->imageStyleStorage
->load($main_image_style_setting);
}
$modal_image_style_setting = $this
->getSetting('modal_image_style');
if (!empty($modal_image_style_setting)) {
$modal_image_style = $this->imageStyleStorage
->load($modal_image_style_setting);
}
foreach ($files as $delta => $file) {
$image_uri = $file
->getFileUri();
$url = Url::fromUri(file_create_url($image_uri));
$item = $file->_referringItem;
$item_attributes = $item->_attributes;
unset($item->_attributes);
if ($delta == 0) {
$cache_tags = [];
if (isset($main_image_style)) {
$cache_tags = $main_image_style
->getCacheTags();
}
$modal_cache_tags = [];
if (isset($modal_image_style)) {
$modal_cache_tags = $modal_image_style
->getCacheTags();
}
$imageGallery['#main'] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $main_image_style_setting,
'#cache' => [
'tags' => Cache::mergeTags($cache_tags, $file
->getCacheTags()),
],
];
}
if (isset($main_image_style)) {
$item_attributes['data-mainsrc'] = $main_image_style
->buildUrl($image_uri);
}
else {
$item_attributes['data-mainsrc'] = file_create_url($image_uri);
}
if ($thumbnailsPerRow > 0) {
$imageGallery['#thumbnails']['images'][] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $thumbnail_image_style,
'#cache' => [
'tags' => Cache::mergeTags($thumbnail_cache_tags, $file
->getCacheTags()),
],
];
}
$imageGallery['#carousel']['images'][$delta] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $modal_image_style_setting,
'#caption' => $item
->__get('alt'),
'#cache' => [
'tags' => Cache::mergeTags($modal_cache_tags, $file
->getCacheTags()),
],
];
if ($this
->getSetting('lazyload')) {
$imageGallery['#carousel']['images'][$delta]['#attributes']['data-src'] = $url
->toString();
}
else {
$imageGallery['#carousel']['images'][$delta]['#uri'] = $image_uri;
}
}
return [
$imageGallery,
];
}
public function settingsSummary() {
$summary[] = $this
->t('Image Style: <strong>@settingValue</strong>', [
'@settingValue' => empty($this
->getSetting('image_style')) ? 'Original Image' : $this
->getSetting('image_style'),
]);
$summary[] = $this
->t('Thumbnails Per Row: <strong>@settingValue</strong>', [
'@settingValue' => $this
->getSetting('thumbnails_per_row'),
]);
$summary[] = $this
->t('Thumbnail Image Style: <strong>@settingValue</strong>', [
'@settingValue' => empty($this
->getSetting('thumbnail_image_style')) ? 'Original Image' : $this
->getSetting('thumbnail_image_style'),
]);
$summary[] = $this
->t('Modal Image Style: <strong>@settingValue</strong>', [
'@settingValue' => empty($this
->getSetting('modal_image_style')) ? 'Original Image' : $this
->getSetting('modal_image_style'),
]);
$summary[] = $this
->t('Autorotate Carousel? <strong>@settingValue</strong>', [
'@settingValue' => $this
->getSetting('carousel_autorotate') ? 'Yes' : 'No',
]);
if ($this
->getSetting('carousel_autorotate')) {
$summary[] = $this
->t('Autorotate Carousel Interval <strong>@settingValue</strong>', [
'@settingValue' => empty($this
->getSetting('carousel_interval')) ? 5000 : $this
->getSetting('carousel_interval'),
]);
}
$summary[] = $this
->t('Lazy Load Images? <strong>@settingValue</strong>', [
'@settingValue' => $this
->getSetting('lazyload') ? 'Yes' : 'No',
]);
return $summary;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$image_styles = image_style_options(FALSE);
$elements = [];
$elements['image_style'] = [
'#title' => $this
->t('Image style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#options' => $image_styles,
'#description' => $this
->t('Image style used for rendering the main image.'),
];
$elements['thumbnails_per_row'] = [
'#type' => 'number',
'#title' => $this
->t('Thumbnails Per Row'),
'#description' => $this
->t('Number of thumbnails displayed per row under the main image.'),
'#min' => 0,
'#default_value' => $this
->getSetting('thumbnails_per_row'),
];
$elements['thumbnail_image_style'] = [
'#title' => $this
->t('Thumbnail Image style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('thumbnail_image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#options' => $image_styles,
'#description' => $this
->t('Image style used for rendering the thumbnails.'),
];
$elements['modal_image_style'] = [
'#title' => $this
->t('Modal Image style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('modal_image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#options' => $image_styles,
'#description' => $this
->t('Image style used for rendering the image in the modal popup (on click).'),
];
$elements['carousel_autorotate'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Autorotate Carousel?'),
'#description' => $this
->t('Decides whether or not the carousel auto-rotates after opening.'),
'#default_value' => $this
->getSetting('carousel_autorotate'),
];
$elements['carousel_interval'] = [
'#type' => 'number',
'#title' => $this
->t('Autorotate Carousel Interval'),
'#description' => $this
->t('The amount of time to delay (in milliseconds) between automatically cycling an image.'),
'#default_value' => $this
->getSetting('carousel_interval'),
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition
->getName() . '][settings_edit_form][settings][carousel_autorotate]"]' => [
'checked' => TRUE,
],
],
],
];
$elements['lazyload'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Lazy Load Images?'),
'#description' => $this
->t('Decides whether or not the images in the popup will be lazy loaded. If yes, the images will not be loaded by the user until they are viewed. This speeds up page loading time.'),
'#default_value' => $this
->getSetting('lazyload'),
];
return $elements;
}
public static function defaultSettings() {
return [
'image_style' => '',
'thumbnail_image_style' => '',
'thumbnails_per_row' => '3',
'modal_image_style' => '',
'carousel_autorotate' => 0,
'carousel_interval' => 5000,
'lazyload' => 1,
] + parent::defaultSettings();
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
foreach ([
'image_style',
'thumbnail_image_style',
'modal_image_style',
] as $setting_name) {
$style_id = $this
->getSetting($setting_name);
if ($style_id && ($style = ImageStyle::load($style_id))) {
$dependencies[$style
->getConfigDependencyKey()][] = $style
->getConfigDependencyName();
}
}
return $dependencies;
}
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
foreach ([
'image_style',
'thumbnail_image_style',
'modal_image_style',
] as $setting_name) {
$style_id = $this
->getSetting($setting_name);
if ($style_id && ($style = ImageStyle::load($style_id))) {
if (!empty($dependencies[$style
->getConfigDependencyKey()][$style
->getConfigDependencyName()])) {
$replacement_id = $this->imageStyleStorage
->getReplacementId($style_id);
if ($replacement_id && ImageStyle::load($replacement_id)) {
$this
->setSetting($setting_name, $replacement_id);
$changed = TRUE;
}
}
}
}
return $changed;
}
}