You are here

function theme_hover_preview_formatter in Hover Preview for ImageCache 6

Display a field like an imagecache formatter but with a wrapper to facilitate the hover preview.

Some parts of this function are inspired from theme_imagecache_formatter_default() since hover preview is just extra stuff around an imagecache-generated img tag.

1 string reference to 'theme_hover_preview_formatter'
hover_preview_theme in ./hover_preview.module
Implementation of hook_theme().

File

./hover_preview.module, line 75
Hover Preview for Imagecache.

Code

function theme_hover_preview_formatter($element) {

  // Generate unique id's for an image and it's corresponding preview url
  $unique = $element['#item']['fid'] . '-' . $element['#item']['timestamp'];

  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }
  drupal_add_js(drupal_get_path('module', 'hover_preview') . '/hover_preview.js');
  drupal_add_css(drupal_get_path('module', 'hover_preview') . '/hover_preview.css');

  // Extract the two constituent presets
  if (substr($element['#formatter'], strlen($element['#formatter']) - 7, strlen($element['#formatter'])) == '_linked') {
    $linked = TRUE;
    list($preset_a, $preset_b) = explode('_hover_preview_', substr($element['#formatter'], 0, strlen($element['#formatter']) - 7));
  }
  else {
    list($preset_a, $preset_b) = explode('_hover_preview_', $element['#formatter']);
  }

  // Generate the preview data
  $preview_id = "hover-preview-" . $unique;
  $preview_url = imagecache_create_url($preset_b, $element['#item']['filepath']);
  $item = $element['#item'];
  $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : '';
  $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : '';
  $preview_text = theme('imagecache', $preset_b, $element['#item']['filepath'], $item['data']['alt'], $item['data']['title'], array(
    'class' => 'hover-preview-preview-url',
    'style' => 'display: none',
    'id' => $preview_id . '-url',
  ));

  // Generate stuff needed for imagecache
  $style = 'default';
  $item = $element['#item'];

  // Set the image alt and title if suitable values are available
  if (!empty($item['data']['alt'])) {
    $alt = $item['data']['alt'];
  }
  elseif (isset($element['#node']->node_title)) {
    $alt = $element['#node']->node_title;
  }
  elseif (isset($element['#node']->title)) {
    $alt = $element['#node']->title;
  }
  if (!empty($item['data']['title'])) {
    $title = $item['data']['title'];
  }
  elseif (isset($element['#node']->node_title)) {
    $title = $element['#node']->node_title;
  }
  else {
    $title = $alt;
  }

  // Generate the imagecache html
  $class = "imagecache imagecache-{$preset_a} imagecache-{$style} imagecache-{$element['#formatter']}";
  $class .= " hover-preview";
  $output = theme('imagecache', $preset_a, $item['filepath'], $alt, $title, array(
    'class' => $class,
    'id' => $preview_id,
  ));
  if ($linked == TRUE && isset($element['#node']->path)) {
    $return = l($output, $element['#node']->path, array(
      'html' => array(
        'html' => 'true',
      ),
    )) . $preview_text;
  }
  else {
    $return = $output . $preview_text;
  }
  return $return;
}