You are here

function linkimagefield_field in Link Image Field 5

Implementation of hook_field().

File

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

Code

function linkimagefield_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 = _linkimagefield_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) {
        if ($item['url'] == "") {
          $item['url'] = "node/" . $node->nid;
        }
        $node_field[$delta] = linkimagefield_file_insert($node, $item, $field);

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

    // called before content.module defaults.
    case 'update':
      foreach ($node_field as $delta => $item) {
        if ($item['url'] == "") {
          $item['url'] = "node/" . $node->nid;
        }

        // 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.
        // Update each file item.
        $node_field[$delta] = linkimagefield_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] = linkimagefield_default_item();
          }
        }
      }

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