You are here

ElevateZoomPlusManager.php in ElevateZoom Plus 7

Same filename and directory in other branches
  1. 8 src/ElevateZoomPlusManager.php

File

src/ElevateZoomPlusManager.php
View source
<?php

namespace Drupal\elevatezoomplus;

use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyManagerInterface;
use Drupal\elevatezoomplus\Entity\ElevateZoomPlus;

/**
 * Provides ElevateZoom Plus library methods mainly for hooks.
 */
class ElevateZoomPlusManager {

  /**
   * The libraries definition.
   *
   * @var array
   */
  protected $libraries;

  /**
   * The blazy manager service.
   *
   * @var \Drupal\blazy\BlazyManagerInterface
   */
  protected $manager;

  /**
   * Constructs a ElevateZoomPlusManager instance.
   */
  public function __construct(BlazyManagerInterface $manager) {
    $this->manager = $manager;
  }

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

  /**
   * Checks if the requirements are met.
   */
  public function isApplicable(array $settings) {

    // @todo remove first_uri for _uri for consistency.
    return !empty($settings['elevatezoomplus']) && (!empty($settings['_uri']) || !empty($settings['first_uri']) || !empty($settings['uri']));
  }

  /**
   * Implements hook_blazy_form_element_alter().
   */
  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',
    ]);

    // Exclude from blazy text formatters, or blazy views grid.
    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,
      ];

      // Hooks into Blazy UI to support Blazy Filter.
      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',
            ];
          }
        }
      }
    }
  }

  /**
   * Return the options for the JSON object.
   */
  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 not using Slick Carousel, provides a static grid gallery.
    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;
  }

  /**
   * Sets ElevateZoomPlus #pre_render callback.
   */
  public function buildAlter(array &$build, array $settings = []) {
    if ($this
      ->isApplicable($settings)) {
      $build['#pre_render'][] = 'elevatezoomplus_pre_render_build';
    }
  }

  /**
   * Implements hook_blazy_attach_alter().
   */
  public function attachAlter(array &$load, array $attach = []) {
    $load['js'][] = [
      'data' => [
        'elevateZoomPlus' => ElevateZoomPlus::defaultSettings(),
      ],
      'type' => 'setting',
    ];
    $load['library'][] = [
      'elevatezoomplus',
      'load',
    ];
  }

  /**
   * Overrides variables for theme_blazy().
   */
  public function preprocessBlazy(&$variables) {
    $settings = $variables['settings'];
    $zoom_url = $variables['url'];

    // Support video thumbnail since `url` points to a provider site.
    if ($settings['type'] == 'video' && !empty($settings['box_url'])) {
      $zoom_url = $settings['box_url'];
    }

    // Re-use thumbnail style for the stage/ preview image.
    $stage_url = empty($variables['attributes']['data-thumb']) ? $zoom_url : $variables['attributes']['data-thumb'];

    // Provides the expected attributes for JS.
    $variables['url_attributes']['data-image'] = $stage_url;
    $variables['url_attributes']['data-zoom-image'] = $zoom_url;

    // If using Slick asNavFor, make the litebox link as a zoom trigger as well.
    $id = Blazy::getHtmlId('elevatezoomplus');
    if (!empty($settings['nav'])) {
      $variables['url_attributes']['class'][] = 'elevatezoomplus';
      $variables['url_attributes']['id'] = $id;
    }
    else {
      $variables['item_attributes']['id'] = $id;
    }
  }

}

Classes

Namesort descending Description
ElevateZoomPlusManager Provides ElevateZoom Plus library methods mainly for hooks.