You are here

function imagefield_field in ImageField 5.2

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

Implementation of hook_field().

1 call to imagefield_field()
imagefield_js in ./imagefield.module
Menu-callback for JavaScript-based uploads.

File

./imagefield.module, line 301
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($op, $node, $field, &$items, $teaser, $page) {
  $fieldname = $field['field_name'];
  switch ($op) {

    // called after content.module loads default data.
    case 'load':
      if (!count($items)) {
        return;
      }
      foreach ($items as $delta => $item) {
        if (empty($item)) {
          unset($items[$delta]);
        }
        elseif (!empty($item['fid'])) {
          $items[$delta] = array_merge($item, _imagefield_file_load($item['fid']));
        }
      }
      $items = array_values(array_filter($items));

      // compact deltas
      return array(
        $fieldname => $items,
      );
      break;

    // called before content.module defaults.
    case 'insert':
      foreach ($items as $delta => $item) {
        if ($item['flags']['delete']) {
          unset($items[$delta]);
        }
        else {
          $items[$delta] = imagefield_file_insert($node, $item, $field);
        }
      }
      $items = array_values(array_filter($items));

      // compact deltas
      imagefield_clear_field_session($fieldname);
      break;

    // called before content.module defaults.
    case 'update':
      foreach ($items as $delta => $item) {

        // If we're dealing with a single value field, and we just received
        // a new file item, we need to mark the existing (old) one for
        // deletion.  Otherwise, it will become orphaned.
        if (!$field['multiple'] && !empty($items) && count($items) > 1 && $delta === 0) {
          $item['flags']['hidden'] = true;
          $item['flags']['delete'] = true;
        }

        // Update each file item.
        $items[$delta] = imagefield_file_update($node, $item, $field);

        // If the file has been deleted, unset the file entry so that it's
        // actually deleted from the database, or at least set it to a
        // default item if CCK won't delete it.
        if (empty($items[$delta])) {
          if ($field['multiple']) {
            unset($items[$delta]);
          }
        }
      }
      $items = array_values(array_filter($items));

      // compact deltas
      imagefield_clear_field_session($fieldname);
      break;
    case 'delete revision':
      $db_info = content_database_info($field);
      foreach ($items as $delta => $item) {
        $references = db_result(db_query("SELECT count(vid) FROM {%s} WHERE nid=%d AND %s=%d and vid!=%d", $db_info['table'], $node->nid, $db_info['columns']['fid']['column'], $item['fid'], $node->vid));
        if ($references || _imagefield_file_delete($item, $field['field_name'])) {
          $items[$delta] = array();
        }
      }
      $items = array_values($items);

      // compact deltas
      break;
    case 'delete':
      foreach ($items as $delta => $item) {
        _imagefield_file_delete($item, $field['field_name']);
      }
      break;
    case 'view':
      $context = $teaser ? 'teaser' : 'full';
      $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
      foreach ($items as $delta => $item) {
        if ($item['fid'] == 0) {
          unset($items[$delta]);
        }
      }
      if ($field['use_default_image'] && empty($items)) {
        $items[0] = $field['default_image'];
      }
      foreach ($items as $delta => $item) {
        $items[$delta]['view'] = content_format($field, $item, $formatter, $node);
      }
      return theme('field', $node, $field, $items, $teaser, $page);
  }
}