You are here

elevatezoomplus.theme.inc in ElevateZoom Plus 8

Same filename and directory in other branches
  1. 7 elevatezoomplus.theme.inc

Hooks and preprocess functions for the Blazy module.

File

elevatezoomplus.theme.inc
View source
<?php

/**
 * @file
 * Hooks and preprocess functions for the Blazy module.
 */
use Drupal\Component\Serialization\Json;
use Drupal\blazy\Blazy;

/**
 * Prepares variables for elevatezoomplus.html.twig templates.
 *
 * @see Drupal\commerce_product\ProductVariationFieldRenderer
 */
function template_preprocess_elevatezoomplus(array &$variables) {
  $element = $variables['element'];
  foreach ([
    'attributes',
    'content',
    'settings',
  ] as $key) {
    $variables[$key] = isset($element['#' . $key]) ? $element['#' . $key] : [];
  }
  $attributes =& $variables['attributes'];
  $settings = $variables['settings'];
  $json = elevatezoomplus()
    ->getOptions($settings);

  // Remove problematic container class which cause multiple AJAX classes due
  // to using the same attributes array affecting this theme.
  $class = isset($settings['ajax_replace_class']) ? $settings['ajax_replace_class'] : '';
  if ($class && isset($attributes['class'])) {
    $attributes['class'] = array_diff((array) $attributes['class'], [
      $class,
    ]);
  }
  $attributes['data-elevatezoomplus'] = Json::encode($json);
  $variables['content_attributes'] = [
    'data-blazy' => '',
  ];

  // Checks if Splide/ Slick asNavFor, or regular carousels, Blazy Grid, etc.
  // Only provides static stage if not using Splide/Slick asNavFor.
  $variables['stage'] = empty($settings['nav']) ? elevatezoomplus_build($settings) : [];
}

/**
 * Build the main gallery image/ stage if not using Slick asNavFor.
 *
 * @todo remove first_item, first_uri (BC with older Blazy 2) at Blazy 3.x.
 */
function elevatezoomplus_build(array $settings = []) {
  $uri = isset($settings['_uri']) ? $settings['_uri'] : (isset($settings['first_uri']) ? $settings['first_uri'] : '');
  $item = isset($settings['first_item']) ? $settings['first_item'] : '';
  $item = isset($settings['_item']) ? $settings['_item'] : $item;
  $url = empty($settings['box_style']) ? file_url_transform_relative(file_create_url($uri)) : blazy()
    ->entityLoad($settings['box_style'])
    ->buildUrl($uri);
  $style = empty($settings['thumbnail_style']) ? '' : $settings['thumbnail_style'];
  $param = [
    'image_style' => $style,
    'lazy' => 'blazy',
    'uri' => $uri,
  ];
  $id = Blazy::getHtmlId('elevatezoomplus');
  $zoom = [
    'data-zoom-image' => $url,
  ];
  $attrs = [
    'class' => [
      'elevatezoomplus elevatezoomplus--stage',
    ],
    'id' => $id,
  ] + $zoom;
  $build = [
    'item' => $item,
    'settings' => $param,
    'item_attributes' => $attrs,
  ];
  return [
    '#type' => 'inline_template',
    '#template' => '{{ stage }}',
    '#context' => [
      'stage' => blazy()
        ->getBlazy($build),
    ],
    '#attached' => blazy()
      ->attach($param + $settings),
  ];
}

Functions

Namesort descending Description
elevatezoomplus_build Build the main gallery image/ stage if not using Slick asNavFor.
template_preprocess_elevatezoomplus Prepares variables for elevatezoomplus.html.twig templates.