View source
<?php
namespace Drupal\elevatezoomplus;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyManagerInterface;
use Drupal\elevatezoomplus\Entity\ElevateZoomPlus;
class ElevateZoomPlusManager implements TrustedCallbackInterface {
use StringTranslationTrait;
protected $manager;
protected $optionsetOptions;
public function __construct(BlazyManagerInterface $manager) {
$this->manager = $manager;
}
public static function trustedCallbacks() {
return [
'preRenderBuild',
];
}
public function manager() {
return $this->manager;
}
public function libraryInfoAlter(&$libraries, $extension) {
$library = elevatezoomplus_libraries_get_path('elevatezoom-plus') ?: elevatezoomplus_libraries_get_path('ez-plus');
if ($library) {
$ext = is_file($library . '/src/jquery.ez-plus.min.js') ? 'min.js' : 'js';
$libraries['elevatezoomplus']['js']['/' . $library . '/src/jquery.ez-plus.' . $ext] = [
'weight' => -5,
];
if ($this->manager
->getModuleHandler()
->moduleExists('splide')) {
$libraries['load']['dependencies'][] = 'splide/load';
$libraries['load']['dependencies'][] = 'splide/nav';
}
if ($this->manager
->getModuleHandler()
->moduleExists('slick')) {
$libraries['load']['dependencies'][] = 'slick/slick.load';
}
}
}
public function isApplicable(array $settings) {
return !empty($settings['elevatezoomplus']) && (!empty($settings['_uri']) || !empty($settings['uri']));
}
public function getOptionsetOptions($entity_type = '') {
if (!isset($this->optionsetOptions)) {
$optionsets = [];
foreach ($this->manager
->entityLoadMultiple($entity_type) as $key => $entity) {
$optionsets[$key] = strip_tags($entity
->label());
}
$this->optionsetOptions = $optionsets;
}
return $this->optionsetOptions;
}
public function formElementAlter(array &$form, array $definition = []) {
$field_type = isset($definition['field_type']) ? $definition['field_type'] : '';
$settings = $definition['settings'];
$texts = [
'link',
'string',
'string_long',
];
$applicable = $field_type && !in_array($field_type, $texts);
if (empty($definition['no_image_style']) && !isset($settings['grouping'])) {
$elevatezoomplus = [
'#type' => 'select',
'#title' => $this
->t('ElevateZoom Plus'),
'#options' => $this
->getOptionsetOptions('elevatezoomplus'),
'#empty_option' => $this
->t('- None -'),
'#default_value' => isset($settings['elevatezoomplus']) ? $settings['elevatezoomplus'] : '',
'#description' => $this
->t('Choose an optionset.'),
'#weight' => -98.98999999999999,
'#enforce' => FALSE,
];
if (isset($settings['admin_css'])) {
$form['extras']['#access'] = TRUE;
$form['extras']['elevatezoomplus'] = $elevatezoomplus;
$form['extras']['elevatezoomplus']['#default_value'] = isset($settings['extras']['elevatezoomplus']) ? $settings['extras']['elevatezoomplus'] : '';
$form['extras']['elevatezoomplus']['#description'] .= ' ' . $this
->t('Blazy/Splide/Slick Filter only. Warning! Not working nicely. This needs extra image styles which are lacking with inline images.');
}
else {
if ($applicable) {
$form['elevatezoomplus'] = $elevatezoomplus;
$form['elevatezoomplus']['#description'] .= ' ' . $this
->t('Requires any lightbox (<b>not: Image to iFrame, Image linked to content, Image rendered</b>) for <b>Media switcher</b> if using Splide/Slick with asNavFor. If not, be sure to choose only <b>Image to Elevatezoomplus</b>.');
if ($this->manager
->configLoad('admin_css', 'blazy.settings')) {
$form['closing']['#attached']['library'][] = 'elevatezoomplus/admin';
}
}
}
}
}
public function getOptions(array $settings = []) {
$config = $this->manager
->configLoad();
$fallback = empty($config['extras']['elevatezoomplus']) ? 'default' : $config['extras']['elevatezoomplus'];
$plugin_id = isset($settings['plugin_id']) ? $settings['plugin_id'] : '';
$filters = [
'blazy_filter',
'splide_filter',
'slick_filter',
];
$option_id = $plugin_id && in_array($plugin_id, $filters) ? $fallback : $settings['elevatezoomplus'];
$optionset = ElevateZoomPlus::loadWithFallback($option_id);
$options = $optionset
->getSettings(TRUE);
if (empty($settings['nav'])) {
$options['galleryItem'] = '[data-elevatezoomplus-trigger]';
$options['galleryActiveClass'] = 'is-active';
$options['gallerySelector'] = '[data-elevatezoomplus-gallery]';
}
if (isset($options['zoomWindowPosition']) && is_numeric($options['zoomWindowPosition'])) {
$options['zoomWindowPosition'] = (int) $options['zoomWindowPosition'];
}
if (empty($options['loadingIcon'])) {
unset($options['loadingIcon']);
}
$namespace = isset($settings['namespace']) ? $settings['namespace'] : '';
$behaviors = [];
if ($namespace) {
if ($namespace == 'splide') {
$behaviors = [
'splide',
'splideNav',
];
}
elseif ($namespace == 'slick') {
$behaviors = [
'slick',
];
}
}
if (isset($settings['media_switch']) && ($settings['media_switch'] = 'media')) {
$behaviors[] = 'blazyMedia';
}
if (isset($settings['lightbox']) && ($lightbox = $settings['lightbox'])) {
foreach ($this->manager
->getLightboxes() as $key) {
if ($lightbox == $key) {
switch ($key) {
case 'colorbox':
$behaviors[] = 'blazyColorbox';
break;
case 'photobox':
$behaviors[] = 'blazyPhotobox';
break;
case 'photoswipe':
$behaviors[] = 'blazyPhotoSwipe';
break;
case 'slick_lightbox':
$behaviors[] = 'slickLightbox';
break;
default:
$behaviors[] = $key;
break;
}
}
}
}
if ($behaviors) {
$options['behaviors'] = $behaviors;
}
return $options;
}
public function buildAlter(array &$build, $settings = []) {
if ($this
->isApplicable($settings)) {
$build = [
'#theme' => 'elevatezoomplus',
'#build' => $build,
'#pre_render' => [
[
$this,
'preRenderBuild',
],
],
];
}
}
public function preRenderBuild($element) {
$build = $element['#build'];
unset($element['#build']);
$settings = [];
foreach ([
'blazy',
'build',
'context',
'settings',
] as $key) {
if (isset($build['#' . $key])) {
$settings = isset($build['#' . $key]['settings']) ? $build['#' . $key]['settings'] : [];
$settings = $key == 'build' || $key == 'context' ? $settings : $build['#' . $key];
break;
}
}
$attributes = isset($element['#container_attributes']) ? (array) $element['#container_attributes'] : [];
$ajax_class = isset($element['#ajax_replace_class']) ? $element['#ajax_replace_class'] : '';
if (empty($ajax_class)) {
if (isset($build['#attributes'], $build['#attributes']['class'])) {
foreach ($build['#attributes']['class'] as $key => $class) {
if (mb_strpos($class, 'product--variation-field--variation') !== FALSE) {
$attributes['class'][] = $class;
break;
}
}
}
}
else {
$attributes['class'][] = $ajax_class;
}
$settings['ajax_replace_class'] = $ajax_class;
$element['#settings'] = $settings;
$element['#attributes'] = $attributes;
$element['#content'] = $build;
unset($build);
return $element;
}
public function attachAlter(array &$load, $attach = []) {
$load['drupalSettings']['elevateZoomPlus'] = ElevateZoomPlus::defaultSettings();
$load['library'][] = 'elevatezoomplus/load';
}
public function preprocessBlazy(&$variables) {
$settings = $variables['settings'];
$zoom_url = $variables['url'];
if ($settings['type'] == 'video' && !empty($settings['box_url'])) {
$zoom_url = $settings['box_url'];
}
$stage_url = empty($variables['attributes']['data-thumb']) ? $zoom_url : $variables['attributes']['data-thumb'];
$variables['url_attributes']['data-image'] = $stage_url;
$variables['url_attributes']['data-zoom-image'] = $zoom_url;
$id = Blazy::getHtmlId('elevatezoomplus');
if (!empty($settings['nav'])) {
$variables['url_attributes']['class'][] = 'elevatezoomplus';
$variables['url_attributes']['id'] = $id;
}
else {
$variables['item_attributes']['id'] = $id;
}
}
}