You are here

function imagefield_field_formatter in ImageField 5.2

Same name and namespace in other branches
  1. 5 imagefield.module \imagefield_field_formatter()

Implementation of hook_field_formatter().

File

./imagefield.module, line 950
Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.

Code

function imagefield_field_formatter($field, $item, $formatter, $node = null) {

  // Use a default image if available.
  if (empty($item['fid']) && $field['use_default_image']) {
    $item = $field['default_image'];
  }

  // If a filepath isn't loaded, see if we can load one.
  if (!empty($item['fid']) && empty($item['filepath'])) {
    $item = array_merge($item, _imagefield_file_load($item['fid']));
  }

  // If we don't have a filepath at this point, no point in continuing.
  if (empty($item['filepath'])) {
    return '';
  }
  $parts = explode('_', $formatter);
  $style = array_pop($parts);
  $fieldtype = implode('_', $parts);
  $class = 'imagefield imagefield-' . $field['field_name'];
  switch ($style) {
    case 'imagelink':
      $original_image_url = file_create_url($item['filepath']);
      $imagetag = theme('imagefield_image', $item, $item['alt'], $item['title'], array(
        'class' => $class,
      ));
      $class .= ' imagefield-imagelink';
      return l($imagetag, $original_image_url, array(
        'class' => $class,
      ), null, null, false, true);
    case 'nodelink':
      $imagetag = theme('imagefield_image', $item, $item['alt'], $item['title'], array(
        'class' => $class,
      ));
      $class .= ' imagefield-nodelink';
      $id = 'imagefield-nodelink-' . $node->nid;
      return l($imagetag, 'node/' . $node->nid, array(
        'class' => $class,
        'id' => $id,
      ), null, null, false, true);
    case 'url':
      return theme('imagefield_formatter_url', file_create_url($item['filepath']), array(
        'class' => $class,
      ));
    case 'path':
      return theme('imagefield_formatter_path', file_create_path($item['filepath']), array(
        'class' => $class,
      ));
    default:
      return theme('imagefield_image', $item, $item['alt'], $item['title'], array(
        'class' => $class,
      ));
  }
}