View source
<?php
namespace Drupal\elevatezoomplus;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyManagerInterface;
use Drupal\elevatezoomplus\Entity\ElevateZoomPlus;
class ElevateZoomPlusManager {
protected $libraries;
protected $manager;
public function __construct(BlazyManagerInterface $manager) {
$this->manager = $manager;
}
public function library() {
if (!isset($this->libraries)) {
$info = system_get_info('module', 'elevatezoomplus');
$library = libraries_get_path('elevatezoom-plus') ?: libraries_get_path('ez-plus');
$path = drupal_get_path('module', 'elevatezoomplus');
$common = [
'website' => 'https://drupal.org/project/elevatezoomplus',
'version' => empty($info['version']) ? '7.x-1.x' : $info['version'],
];
$ext = is_file($library . '/src/jquery.ez-plus.min.js') ? 'min.js' : 'js';
$libraries['elevatezoomplus'] = [
'title' => 'ElevateZoom Plus',
'website' => 'https://github.com/igorlino/elevatezoom-plus',
'js' => [
$library . '/src/jquery.ez-plus.' . $ext => [
'group' => JS_LIBRARY,
],
],
'version' => '1.x',
'dependencies' => [
[
'system',
'jquery',
],
],
];
$libraries['load'] = [
'css' => [
$path . '/css/elevatezoomplus.css' => [],
],
'js' => [
$path . '/js/elevatezoomplus.load.min.js' => [
'group' => JS_DEFAULT,
'weight' => -0.03,
],
],
'dependencies' => [
[
'blazy',
'blazybox',
],
[
'elevatezoomplus',
'elevatezoomplus',
],
],
];
$libraries['admin'] = [
'css' => [
$path . '/css/elevatezoomplus.admin.css' => [],
],
'dependencies' => [
[
'blazy',
'admin',
],
],
];
foreach ($libraries as &$library) {
$library += $common;
if (isset($library['js'])) {
$library['dependencies'][] = [
'system',
'jquery.once',
];
}
}
$this->libraries = $libraries;
}
return $this->libraries;
}
public function isApplicable(array $settings) {
return !empty($settings['elevatezoomplus']) && (!empty($settings['_uri']) || !empty($settings['first_uri']) || !empty($settings['uri']));
}
public function formElementAlter(array &$form, array $definition = []) {
$field_type = isset($definition['field_type']) ? $definition['field_type'] : '';
$settings = $definition['settings'];
$applicable = $field_type && in_array($field_type, [
'file',
'image',
]);
if (empty($definition['no_image_style']) && !isset($settings['grouping'])) {
$optionsets = ElevateZoomPlus::loadMultiple();
$elevatezoomplus = [
'#type' => 'select',
'#title' => t('ElevateZoom Plus'),
'#options' => $this->manager
->getOptionsetOptions($optionsets),
'#empty_option' => t('- None -'),
'#default_value' => isset($settings['elevatezoomplus']) ? $settings['elevatezoomplus'] : '',
'#description' => 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'] .= ' ' . t('Blazy 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'] .= ' ' . t('Requires any lightbox (<b>not: Image to iFrame, Image linked to content, Image rendered</b>) for <b>Media switcher</b> if using Slick Carousel with asNavFor. If not, be sure to choose only <b>Image to Elevatezoomplus</b>.');
if ($this->manager
->config('admin_css', TRUE, 'blazy.settings')) {
$form['closing']['#attached']['library'][] = [
'elevatezoomplus',
'admin',
];
}
}
}
}
}
public function getOptions(array $settings = []) {
$plugin_id = isset($settings['plugin_id']) ? $settings['plugin_id'] : '';
$option_id = $plugin_id == 'blazy_filter' ? $this->manager
->config('extras.elevatezoomplus', 'default') : $settings['elevatezoomplus'];
$optionset = ElevateZoomPlus::load($option_id);
$options = $optionset
->getSettings(TRUE);
if (empty($settings['nav'])) {
$options['galleryItem'] = '[data-elevatezoomplus-trigger]';
$options['galleryActiveClass'] = 'is-active';
if (isset($settings['gallery_id'])) {
$options['gallery'] = $settings['gallery_id'];
}
else {
$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']);
}
return $options;
}
public function buildAlter(array &$build, array $settings = []) {
if ($this
->isApplicable($settings)) {
$build['#pre_render'][] = 'elevatezoomplus_pre_render_build';
}
}
public function attachAlter(array &$load, array $attach = []) {
$load['js'][] = [
'data' => [
'elevateZoomPlus' => ElevateZoomPlus::defaultSettings(),
],
'type' => 'setting',
];
$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;
}
}
}