You are here

function hover_preview_field_formatter_view in Hover Preview for ImageCache 7

Implements hook_field_formatter_view().

File

./hover_preview.module, line 110

Code

function hover_preview_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Each hover preview item is created with an image element.
  foreach ($items as $delta => $item) {
    $element[$delta]['#theme'] = 'hover_preview_image_formatter';
    $element[$delta]['#item'] = $item;

    // The title tag.
    if (isset($item['title']) && !empty($item['title'])) {
      $element[$delta]['#image']['title'] = $item['title'];
    }

    // The alt tag.
    if (isset($item['alt']) && !empty($item['alt'])) {
      $element[$delta]['#image']['alt'] = $item['alt'];
    }

    // The image path is contructed based on the image style.
    if (isset($display['settings']['image_style']) && !empty($display['settings']['image_style'])) {
      $element[$delta]['#image']['path'] = image_style_url($display['settings']['image_style'], $item['uri']);
    }
    else {

      // If no image style is provided, we use the original image.
      $element[$delta]['#image']['path'] = $item['uri'];
    }

    // The hover preview image style.
    if (isset($display['settings']['hover_preview_style']) && !empty($display['settings']['hover_preview_style'])) {
      $element[$delta]['#image']['attributes']['data-hover-preview'] = image_style_url($display['settings']['hover_preview_style'], $item['uri']);
    }
    else {

      // If no hover preview style is provided, we use the original image.
      $element[$delta]['#image']['attributes']['data-hover-preview'] = file_create_url($item['uri']);
    }

    // Provide the hover-preview class and the action (default is imgpreview).
    $action = isset($display['settings']['hover_preview_action']) && !empty($display['settings']['hover_preview_action']) ? $display['settings']['hover_preview_action'] : 'imgpreview';
    $element[$delta]['#image']['attributes']['class'][] = 'hover-preview-' . $action;
    $element[$delta]['#image']['attributes']['class'][] = 'hover-preview';

    // Check if the formatter involves a link.
    switch ($display['settings']['image_link']) {
      case 'content':

        // Link to the entity content.
        $uri = entity_uri($entity_type, $entity);
        $element[$delta]['#path'] = array(
          'path' => $uri['path'],
          'options' => array(
            'html' => TRUE,
          ),
        );
        break;
      case 'file':

        // Link directly to the file.
        $element[$delta]['#path'] = array(
          'path' => file_create_url($item['uri']),
          'options' => array(
            'html' => TRUE,
          ),
        );
        break;
    }

    // Special use cases for certain hover preview actions.
    switch ($action) {

      // Image Preview requires the imgPreview library.
      case 'imgpreview':
        $element[$delta]['#attached']['library'][] = array(
          'hover_preview',
          'imgPreview',
        );
        break;

      // Image Preview requires the imgPreview library.
      case 'replace':
        $element[$delta]['#attached']['library'][] = array(
          'hover_preview',
          'imghover',
        );
        break;
        break;
    }

    // The Hover Preview module requires the JavaScript to load the behaviors.
    $element[$delta]['#attached']['js'][] = array(
      'data' => drupal_get_path('module', 'hover_preview') . '/hover_preview.js',
      'type' => 'file',
    );
  }
  return $element;
}