You are here

imagezoom.module in Image Zoom 8.3

Same filename and directory in other branches
  1. 8.2 imagezoom.module
  2. 7.2 imagezoom.module
  3. 7 imagezoom.module

Provides an Image Zoom field formatter for Image fields.

This module provides a field formatter that allows users to specify an image style to display, and another image style to use as the zoomed version of the image. Hovering the mouse over the image will display the zoomed version, which shifts with mouse movement.

File

imagezoom.module
View source
<?php

/**
 * @file
 * Provides an Image Zoom field formatter for Image fields.
 *
 * This module provides a field formatter that allows users to specify an image
 * style to display, and another image style to use as the zoomed version of the
 * image. Hovering the mouse over the image will display the zoomed version,
 * which shifts with mouse movement.
 */
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_theme().
 */
function imagezoom_theme($existing, $type, $theme, $path) {
  return [
    'imagezoom_image' => [
      'variables' => [
        'item' => NULL,
        'display_style' => NULL,
        'zoom_style' => NULL,
        'settings' => NULL,
      ],
      'template' => 'imagezoom-image',
    ],
    'imagezoom_gallery' => [
      'variables' => [
        'items' => NULL,
        'display_style' => NULL,
        'zoom_style' => NULL,
        'thumb_style' => NULL,
        'settings' => NULL,
        'image' => NULL,
        'thumbs' => NULL,
      ],
      'template' => 'imagezoom-gallery',
    ],
    'imagezoom_thumb' => [
      'variables' => [
        'item' => NULL,
        'display_style' => NULL,
        'zoom_style' => NULL,
        'thumb_style' => NULL,
        'settings' => NULL,
      ],
      'template' => 'imagezoom-thumb',
    ],
  ];
}

/**
 * Preprocess function for imagezoom_image.
 */
function template_preprocess_imagezoom_image(&$variables) {
  if ($item = $variables['item']) {

    /** @var Drupal\Core\Image\Image $image */
    if ($variables['display_style']) {
      $image_style = ImageStyle::load($variables['display_style']);
      $variables['image'] = $image_style
        ->buildUrl($item->entity
        ->getFileUri());
      $image = \Drupal::service('image.factory')
        ->get($image_style
        ->buildUri($item->entity
        ->getFileUri()));
    }
    else {
      $variables['image'] = file_create_url($item->entity
        ->getFileUri());
      $image = \Drupal::service('image.factory')
        ->get($item->entity
        ->getFileUri());
    }
    $variables['width'] = $image
      ->getWidth();
    $variables['height'] = $image
      ->getHeight();
    $variables['alt'] = $item->alt;
    $variables['title'] = $item->title;
    if ($variables['zoom_style']) {
      $image_style = ImageStyle::load($variables['zoom_style']);
      $variables['zoom'] = $image_style
        ->buildUrl($item->entity
        ->getFileUri());
    }
    else {
      $variables['zoom'] = file_create_url($item->entity
        ->getFileUri());
    }
  }
}

/**
 * Preprocess function for imagezoom_gallery.
 */
function template_preprocess_imagezoom_gallery(&$variables) {
  if ($items = $variables['items']) {
    $variables['image'] = [
      '#theme' => 'imagezoom_image',
      '#item' => $items[0],
      '#display_style' => $variables['display_style'],
      '#zoom_style' => $variables['zoom_style'],
      '#settings' => $variables['settings'],
    ];
    $variables['thumbs'] = [];
    foreach ($items as $item) {
      $variables['thumbs'][] = [
        '#theme' => 'imagezoom_thumb',
        '#item' => $item,
        '#display_style' => $variables['display_style'],
        '#thumb_style' => $variables['thumb_style'],
        '#zoom_style' => $variables['zoom_style'],
        '#settings' => $variables['settings'],
      ];
    }
  }
}

/**
 * Preprocess function for imagezoom_thumb.
 */
function template_preprocess_imagezoom_thumb(&$variables) {
  $item = $variables['item'];
  if ($variables['display_style']) {
    $image_style = ImageStyle::load($variables['display_style']);
    $variables['image'] = $image_style
      ->buildUrl($item->entity
      ->getFileUri());
  }
  else {
    $variables['image'] = file_create_url($item->entity
      ->getFileUri());
  }
  if ($variables['zoom_style']) {
    $image_style = ImageStyle::load($variables['zoom_style']);
    $variables['zoom'] = $image_style
      ->buildUrl($item->entity
      ->getFileUri());
  }
  else {
    $variables['zoom'] = file_create_url($item->entity
      ->getFileUri());
  }

  /** @var Drupal\Core\Image\Image $image */
  if ($variables['thumb_style']) {
    $image_style = ImageStyle::load($variables['thumb_style']);
    $variables['thumb'] = $image_style
      ->buildUrl($item->entity
      ->getFileUri());
    $image = \Drupal::service('image.factory')
      ->get($image_style
      ->buildUri($item->entity
      ->getFileUri()));
  }
  else {
    $variables['thumb'] = file_create_url($item->entity
      ->getFileUri());
    $image = \Drupal::service('image.factory')
      ->get($item->entity
      ->getFileUri());
  }
  $variables['width'] = $image
    ->getWidth();
  $variables['height'] = $image
    ->getHeight();
}

Functions

Namesort descending Description
imagezoom_theme Implements hook_theme().
template_preprocess_imagezoom_gallery Preprocess function for imagezoom_gallery.
template_preprocess_imagezoom_image Preprocess function for imagezoom_image.
template_preprocess_imagezoom_thumb Preprocess function for imagezoom_thumb.