You are here

function theme_lightbox2_image in Lightbox2 8

Same name and namespace in other branches
  1. 5.2 lightbox2.module \theme_lightbox2_image()
  2. 6 lightbox2.formatter.inc \theme_lightbox2_image()
  3. 7.2 lightbox2.formatter.inc \theme_lightbox2_image()
  4. 7 lightbox2.formatter.inc \theme_lightbox2_image()

Theme function for displaying the lightbox2 trigger image in an imagefield.

Parameters

$path: The path to the image to be displayed.

$alt: The image alternative text.

$title: The image title.

$attributes: An array of image attributes, e.g. class name.

Return value

HTML output for displaying the image.

1 theme call to theme_lightbox2_image()
lightbox2_field_formatter_view in ./lightbox2.module
Implements hook_field_formatter_view().

File

./lightbox2.formatter.inc, line 24
Lightbox2 formatter hooks and callbacks.

Code

function theme_lightbox2_image($variables) {
  $path = $variables['path'];
  $item = $variables['item'];
  $image_style = $variables['image_style'];
  $node_id = $variables['node_id'];
  $field_name = $variables['field_name'];
  $caption = $variables['caption'];
  if (!$variables["lightbox_style"]) {
    $path['path'] = file_create_url($item['uri']);
  }
  else {
    $path['path'] = image_style_url($variables["lightbox_style"], $item['uri']);
  }

  // Lightbox type
  $rel = $variables['lightbox_type'];

  // Grouping
  $rel .= '[';
  $group = \Drupal::config('lightbox2.settings')
    ->get('lightbox2_image_group_node_id');
  switch ($group) {

    // No groups
    case '0':
      break;

    // Group by field name
    case '1':
      $rel .= $field_name;
      break;

    // Group by node id
    case '2':
      $rel .= $node_id;
      break;

    // Group by field name and node id
    case '3':
      $rel .= $field_name . '_' . $node_id;
      break;

    // All nodes and fields in same group
    case '4':
      $rel .= 'group';
      break;
  }
  $rel .= ']';

  // Title Start
  $rel .= '[';
  switch ($caption) {
    case 'hidden':
      break;
    case 'filename':
      $rel .= $item['filename'];
      break;
    case 'title':
      $rel .= $item['title'];
      break;
    default:
      $caption_display = array(
        'label' => 'hidden',
      );
      $entity = \Drupal::entityManager()
        ->getStorage('file', array(
        $item['fid'],
      ));
      $entity = $entity[$item['fid']];
      $renderable_field = $entity->{$caption}
        ->view($caption_display);
      $rel .= \Drupal::service("renderer")
        ->render($renderable_field);
      break;
  }

  // Download Link
  if (\Drupal::currentUser()
    ->hasPermission('download original image')) {
    $rel .= '<p><a href="' . file_create_url($item['uri']) . '">' . \Drupal::config('lightbox2.settings')
      ->get('lightbox2_download_link_text') . '</a></p>';
  }

  // Title End
  $rel .= ']';
  $path['options']['attributes']['rel'] = $rel;
  $path['options']['attributes']['title'] = $item['title'];

  // @FIXME
  // theme() has been renamed to _theme() and should NEVER be called directly.
  // Calling _theme() directly can alter the expected output and potentially
  // introduce security issues (see https://www.drupal.org/node/2195739). You
  // should use renderable arrays instead.
  //
  //
  // @see https://www.drupal.org/node/2195739
  // return theme('image_formatter', array( 'item' => $item, 'path' => $path, 'image_style' => $image_style ) );
}