You are here

elevatezoomplus.module in ElevateZoom Plus 7

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

Provides ElevateZoomPlus integration.

File

elevatezoomplus.module
View source
<?php

/**
 * @file
 * Provides ElevateZoomPlus integration.
 */
use Drupal\elevatezoomplus\ElevateZoomPlusManager;

/**
 * Returns one of the ElevateZoomPlus objects mainly used by procedural hooks.
 *
 * @return class
 *   The required ElevateZoomPlus class instance.
 */
function elevatezoomplus() {
  static $manager;
  if (!isset($manager)) {
    $manager = new ElevateZoomPlusManager(blazy());
  }
  return $manager;
}

/**
 * Implements hook_theme().
 */
function elevatezoomplus_theme() {
  return [
    'elevatezoomplus' => [
      'render element' => 'element',
      'file' => 'elevatezoomplus.theme.inc',
    ],
  ];
}

/**
 * Implements hook_ctools_plugin_api().
 */
function elevatezoomplus_ctools_plugin_api($owner, $api) {
  if ($owner == 'elevatezoomplus' && $api == 'elevatezoomplus_optionset') {
    return [
      'version' => 1,
    ];
  }
}

/**
 * Implements hook_library().
 */
function elevatezoomplus_library() {
  return elevatezoomplus()
    ->library();
}

/**
 * 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\\slick\\SlickDefault',
  ];
  if (in_array($context['class'], $classes)) {
    $settings += [
      'elevatezoomplus' => '',
    ];
  }
}

/**
 * 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_ui_build_form_alter(array &$form, $form_state) {
  $definition['settings'] = blazy()
    ->config();
  elevatezoomplus()
    ->formElementAlter($form, $definition);
}

/**
 * The #pre_render callback: Provides ElevateZoomPlus related contents.
 */
function elevatezoomplus_pre_render_build($build) {
  $build['#theme_wrappers'][] = 'elevatezoomplus';
  return $build;
}

/**
 * Implements hook_blazy_build_alter().
 */
function elevatezoomplus_blazy_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);
}

/**
 * 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().
 */
function elevatezoomplus_preprocess_slick(&$variables) {
  $settings = $variables['settings'];
  if (elevatezoomplus()
    ->isApplicable($settings) && isset($settings['display']) && $settings['display'] == 'main') {
    $variables['attributes_array']['data-initial-zoom'] = isset($variables['js']['initialSlide']) ? $variables['js']['initialSlide'] : 0;
  }
}

/**
 * Implements hook_slick_settings_alter().
 */
function elevatezoomplus_slick_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 to always use nav _only if intended to have one.
    // Original Slick only considers a nav if count > 1, this is an enforce.
    $settings['nav'] = !empty($settings['optionset_thumbnail']);

    // Two things to address to with the current Slick 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.
    $settings['count'] = count($items) == 1 ? 2 : count($items);
  }
}