elevatezoomplus-8.x-1.x-dev/elevatezoomplus.module
elevatezoomplus.module
<?php
/**
* @file
* Provides ElevateZoomPlus integration.
*/
use Drupal\elevatezoomplus\ElevateZoomPlusManager;
/**
* Provides a convenient shortcut for procedural hooks.
*
* @return \Drupal\elevatezoomplus\ElevateZoomPlusManager
* The required ElevateZoomPlus class instance.
*/
// @codingStandardsIgnoreStart
function elevatezoomplus(): ElevateZoomPlusManager {
static $manager;
if (!isset($manager)) {
$manager = \Drupal::service('elevatezoomplus.manager');
}
return $manager;
}
// @codingStandardsIgnoreEnd
/**
* Implements hook_theme().
*/
function elevatezoomplus_theme() {
return [
'elevatezoomplus' => [
'render element' => 'element',
'file' => 'elevatezoomplus.theme.inc',
],
];
}
/**
* Implements hook_library_info_alter().
*/
function elevatezoomplus_library_info_alter(&$libraries, $extension) {
if ($extension === 'elevatezoomplus') {
elevatezoomplus()->libraryInfoAlter($libraries, $extension);
}
}
/**
* Implements hook_blazy_attach_alter().
*/
function elevatezoomplus_blazy_attach_alter(array &$load, $attach = []) {
if (!empty($attach['elevatezoomplus'])) {
elevatezoomplus()->attachAlter($load, $attach);
}
}
/**
* Implements hook_blazy_base_settings_alter().
*/
function elevatezoomplus_blazy_base_settings_alter(array &$settings, array $context) {
$settings += ['elevatezoomplus' => ''];
}
/**
* Implements hook_config_schema_info_alter().
*/
function elevatezoomplus_config_schema_info_alter(array &$definitions) {
if (isset($definitions['blazy.settings'])) {
$definitions['blazy.settings']['mapping']['extras']['mapping']['elevatezoomplus']['type'] = 'string';
}
if (isset($definitions['blazy_base'])) {
$definitions['blazy_base']['mapping']['elevatezoomplus']['type'] = 'string';
}
}
/**
* Implements hook_blazy_lightboxes_alter().
*/
function elevatezoomplus_blazy_lightboxes_alter(array &$lightboxes) {
$lightboxes[] = 'elevatezoomplus';
}
/**
* Implements hook_blazy_form_element_alter().
*/
function elevatezoomplus_blazy_form_element_alter(array &$form, array $definition) {
elevatezoomplus()->formElementAlter($form, $definition);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function elevatezoomplus_form_blazy_settings_form_alter(array &$form, $form_state) {
$definition['settings'] = elevatezoomplus()->manager()->config();
elevatezoomplus()->formElementAlter($form, $definition);
}
/**
* Implements hook_blazy_build_alter().
*/
function elevatezoomplus_blazy_build_alter(array &$build, array $settings) {
elevatezoomplus()->buildAlter($build, $settings);
}
/**
* Implements hook_gridstack_build_alter().
*/
function elevatezoomplus_gridstack_build_alter(array &$build, array $settings) {
elevatezoomplus()->buildAlter($build, $settings);
}
/**
* Implements hook_slick_build_alter().
*/
function elevatezoomplus_slick_build_alter(array &$build, array $settings) {
elevatezoomplus()->buildAlter($build, $settings);
}
/**
* Implements hook_splide_build_alter().
*/
function elevatezoomplus_splide_build_alter(array &$build, array $settings) {
elevatezoomplus()->buildAlter($build, $settings);
}
/**
* Overrides variables for theme_blazy().
*/
function elevatezoomplus_preprocess_blazy(&$variables) {
if (elevatezoomplus()->isApplicable($variables['settings'])
&& !empty($variables['url'])) {
elevatezoomplus()->preprocessBlazy($variables);
}
}
/**
* Implements hook_blazy_item_alter().
*/
function elevatezoomplus_blazy_item_alter(array &$settings) {
$blazies = $settings['blazies'];
$is_image = $blazies->get('media.type') === 'image';
if (elevatezoomplus()->isApplicable($settings) && !$is_image) {
$blazies->set('switch', 'blazybox')
// @todo remove post blazy:3.0.9.
->set('lightbox.name', 'blazybox');
}
}
/**
* Overrides variables for theme_[slick|splide]().
*/
function _elevatezoomplus_preprocess_slider(&$variables) {
$settings = $variables['settings'];
if (elevatezoomplus()->isApplicable($settings)
&& ($settings['display'] ?? NULL) == 'main') {
if ($blazies = $settings['blazies'] ?? NULL) {
$namespace = $blazies->get('namespace');
$options = $variables['options'] ?? [];
if ($namespace == 'slick') {
// @todo remove the second check post slick:2.10.
$start = $options['initialSlide'] ?? $variables['js']['initialSlide'] ?? 0;
}
else {
$start = $options['start'] ?? 0;
}
$blazies->set('initial', $start);
$variables['attributes']['data-initial-zoom'] = $start;
}
}
}
/**
* Overrides variables for theme_slick().
*/
function elevatezoomplus_preprocess_slick(&$variables) {
_elevatezoomplus_preprocess_slider($variables);
}
/**
* Overrides variables for theme_splide().
*/
function elevatezoomplus_preprocess_splide(&$variables) {
_elevatezoomplus_preprocess_slider($variables);
}
/**
* Implements hook_slider_settings_alter().
*/
function _elevatezoomplus_slider_settings_alter(array &$build, $object) {
$settings = &$build['#settings'];
// Ensures overrides do not break existing Slick logic for other conditions.
if (elevatezoomplus()->isApplicable($settings)) {
$blazies = $settings['blazies'];
// Forces Slick|Splide to always use nav _only if intended to have one.
// Slick|Splide only considers a nav if count > 1, this is an enforce.
$settings['nav'] = $nav = !empty($settings['optionset_nav'])
|| !empty($settings['optionset_thumbnail']);
// Two things to address to with the current Slick|Splide logic:
// 1. Tricks count > 1 to negate unslick _only if really 1.
// 2. Required count > 1 to display the item wrapper in an unslick mode.
// Known Slick version which doesn't behave with count 1 is Slick 1.6.0.
// Splide has no issues found.
$count = $blazies->total();
if ($object) {
$result = $object->result ?? $object;
$count = count($result) == 1 ? 2 : count($result);
}
$blazies->set('is.nav', $nav)
->set('is.nav_overridden', TRUE)
->set('count', $count)
->set('is.encodedbox', TRUE)
->set('is.richbox', TRUE);
}
}
/**
* Implements hook_blazy_settings_alter().
*/
function elevatezoomplus_blazy_settings_alter(array &$build, $object) {
_elevatezoomplus_slider_settings_alter($build, $object);
}
/**
* Implements hook_slick_settings_alter().
*/
function elevatezoomplus_slick_settings_alter(array &$build, $object) {
_elevatezoomplus_slider_settings_alter($build, $object);
}
/**
* Implements hook_splide_settings_alter().
*/
function elevatezoomplus_splide_settings_alter(array &$build, $object) {
_elevatezoomplus_slider_settings_alter($build, $object);
}
