You are here

function imagecache_field_formatter in ImageCache 5.2

Same name and namespace in other branches
  1. 5 imagecache.module \imagecache_field_formatter()

Implementation of hook_field_formatter().

File

./imagecache.module, line 566
Dynamic image resizer and image cacher.

Code

function imagecache_field_formatter($field, $item, $formatter, $node) {
  if (empty($item['fid']) && $field['use_default_image']) {
    $item = $field['default_image'];
  }

  // Views does not load the file for us, while CCK display fields does.
  if (empty($item['filepath'])) {
    $item = array_merge($item, _imagefield_file_load($item['fid']));
  }

  // If there is no filepath at this point, do nothing.
  if (empty($item['filepath'])) {
    return;
  }
  $parts = explode('_', $formatter);
  $style = array_pop($parts);
  $presetname = implode('_', $parts);
  $class = "imagecache imagecache-{$presetname} imagecache-{$style} imagecache-{$formatter}";
  if ($preset = imagecache_preset_by_name($presetname)) {
    $item['filepath'] = $item['fid'] == 'upload' ? $item['preview'] : $item['filepath'];
    switch ($style) {
      case 'linked':
        $imagetag = theme('imagecache', $presetname, $item['filepath'], $item['alt'], $item['title']);
        return l($imagetag, 'node/' . $node->nid, array(
          'class' => $class,
        ), null, null, false, true);
      case 'imagelink':
        $original_image_url = file_create_url($item['filepath']);
        $imagetag = theme('imagecache', $presetname, $item['filepath'], $item['alt'], $item['title']);
        return l($imagetag, $original_image_url, array(
          'class' => $class,
        ), null, null, false, true);
      case 'url':
        return imagecache_create_url($presetname, $item['filepath']);
      case 'path':
        return imagecache_create_path($presetname, $item['filepath']);
      default:
        return theme('imagecache', $presetname, $item['filepath'], $item['alt'], $item['title']);
    }
  }
  return '<!-- imagecache formatter preset(' . $presetname . ') not found! -->';
}