You are here

function imagefield_field in ImageField 5

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

Implementation of hook_field().

File

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

    // called after content.module loads default data.
    case 'load':
      if (count($node_field)) {
        $values = array();
        foreach ($node_field as $delta => $file) {
          if (!empty($file)) {
            $file = _imagefield_file_load($file['fid']);

            // In certain cases, content.module calls this function with
            // $items[$delta] set, even if the field has not yet been stored at
            // all or has already been deleted. In that case, $file['fid'] == 0
            // and file_load() returns an empty array. When that happens,
            // unset() the delta so that subsequent hooks are not bothered.
            if (empty($file)) {
              unset($node_field[$delta]);
            }
            else {

              // otherwise, merge our info with CCK's, and all is fine.
              $node_field[$delta] = array_merge((array) $node_field[$delta], $file);
            }
          }
        }
        return array(
          $fieldname => $node_field,
        );
      }
      return array();

    // called before content.module defaults.
    case 'insert':
      foreach ($node_field as $delta => $item) {
        $node_field[$delta] = imagefield_file_insert($node, $item, $field);

        // Remove non-existant images from items
        if (empty($node_field[$delta])) {
          unset($node_field[$delta]);
        }
      }
      imagefield_clear_field_session($fieldname);
      break;

    // called before content.module defaults.
    case 'update':
      foreach ($node_field 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'] && count($node_field) > 1 && $delta === 0) {
          $item['flags']['delete'] = TRUE;
        }

        // Update each file item.
        $node_field[$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($node_field[$delta])) {
          if ($field['multiple']) {
            unset($node_field[$delta]);
          }
          else {
            $node_field[$delta] = imagefield_default_item();
          }
        }
      }

      // Compact deltas.
      $node_field = array_values($node_field);
      imagefield_clear_field_session($fieldname);
      break;
    case 'delete':
      foreach ($node_field as $delta => $item) {
        _imagefield_file_delete($item, $field['field_name']);
      }
      break;
  }
}