You are here

elevatezoomplus.module in ElevateZoom Plus 8

Same filename and directory in other branches
  1. 7 elevatezoomplus.module

Provides ElevateZoomPlus integration.

File

elevatezoomplus.module
View source
<?php

/**
 * @file
 * Provides ElevateZoomPlus integration.
 */

/**
 * Provides a convenient shortcut for procedural hooks.
 *
 * @return class
 *   The required ElevateZoomPlus class instance.
 */
function elevatezoomplus() {
  static $manager;
  if (!isset($manager)) {
    $manager = \Drupal::service('elevatezoomplus.manager');
  }
  return $manager;
}

/**
 * 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, $context = []) {
  $classes = [
    'Drupal\\blazy\\BlazyDefault',
    'Drupal\\gridstack\\GridStackDefault',
    'Drupal\\slick\\SlickDefault',
    'Drupal\\splide\\SplideDefault',
  ];
  if (in_array($context['class'], $classes)) {
    $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()
    ->configLoad();
  elevatezoomplus()
    ->formElementAlter($form, $definition);
}

/**
 * Implements hook_blazy_build_alter().
 */
function elevatezoomplus_blazy_build_alter(array &$build, $settings = []) {
  elevatezoomplus()
    ->buildAlter($build, $settings);
}

/**
 * Implements hook_gridstack_build_alter().
 */
function elevatezoomplus_gridstack_build_alter(array &$build, $settings = []) {
  elevatezoomplus()
    ->buildAlter($build, $settings);
}

/**
 * Implements hook_slick_build_alter().
 */
function elevatezoomplus_slick_build_alter(array &$build, $settings = []) {
  elevatezoomplus()
    ->buildAlter($build, $settings);
}

/**
 * Implements hook_splide_build_alter().
 */
function elevatezoomplus_splide_build_alter(array &$build, $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);
  }
}

/**
 * Overrides variables for theme_[slick|splide]().
 */
function _elevatezoomplus_preprocess_slider(&$variables) {
  $settings = $variables['settings'];
  if (elevatezoomplus()
    ->isApplicable($settings) && isset($settings['display']) && $settings['display'] == 'main') {
    $start = isset($variables['js'], $variables['js']['initialSlide']) ? $variables['js']['initialSlide'] : 0;
    $start = isset($variables['options'], $variables['options']['start']) ? $variables['options']['start'] : $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, $items) {
  $settings =& $build['settings'];

  // Ensures overrides do not break existing Slick logic for other conditions.
  if (elevatezoomplus()
    ->isApplicable($settings)) {

    // 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'] = !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.
    $settings['count'] = count($items) == 1 ? 2 : count($items);
  }
}

/**
 * Implements hook_slick_settings_alter().
 */
function elevatezoomplus_slick_settings_alter(array &$build, $items) {
  _elevatezoomplus_slider_settings_alter($build, $items);
}

/**
 * Implements hook_splide_settings_alter().
 */
function elevatezoomplus_splide_settings_alter(array &$build, $items) {
  _elevatezoomplus_slider_settings_alter($build, $items);
}

/**
 * Provides a wrapper to replace deprecated libraries_get_path() at ease.
 *
 * @todo remove and replace with blazy_libraries_get_path() post blazy:8.x-2.0.
 */
function elevatezoomplus_libraries_get_path($name, $base_path = FALSE) {
  return function_exists('blazy_libraries_get_path') ? blazy_libraries_get_path($name, $base_path) : FALSE;
}

Functions

Namesort descending Description
elevatezoomplus Provides a convenient shortcut for procedural hooks.
elevatezoomplus_blazy_attach_alter Implements hook_blazy_attach_alter().
elevatezoomplus_blazy_base_settings_alter Implements hook_blazy_base_settings_alter().
elevatezoomplus_blazy_build_alter Implements hook_blazy_build_alter().
elevatezoomplus_blazy_form_element_alter Implements hook_blazy_form_element_alter().
elevatezoomplus_blazy_lightboxes_alter Implements hook_blazy_lightboxes_alter().
elevatezoomplus_config_schema_info_alter Implements hook_config_schema_info_alter().
elevatezoomplus_form_blazy_settings_form_alter Implements hook_form_FORM_ID_alter().
elevatezoomplus_gridstack_build_alter Implements hook_gridstack_build_alter().
elevatezoomplus_libraries_get_path Provides a wrapper to replace deprecated libraries_get_path() at ease.
elevatezoomplus_library_info_alter Implements hook_library_info_alter().
elevatezoomplus_preprocess_blazy Overrides variables for theme_blazy().
elevatezoomplus_preprocess_slick Overrides variables for theme_slick().
elevatezoomplus_preprocess_splide Overrides variables for theme_splide().
elevatezoomplus_slick_build_alter Implements hook_slick_build_alter().
elevatezoomplus_slick_settings_alter Implements hook_slick_settings_alter().
elevatezoomplus_splide_build_alter Implements hook_splide_build_alter().
elevatezoomplus_splide_settings_alter Implements hook_splide_settings_alter().
elevatezoomplus_theme Implements hook_theme().
_elevatezoomplus_preprocess_slider Overrides variables for theme_[slick|splide]().
_elevatezoomplus_slider_settings_alter Implements hook_slider_settings_alter().