You are here

function filefield_field in FileField 6.2

Same name and namespace in other branches
  1. 5.2 filefield.module \filefield_field()
  2. 5 filefield.module \filefield_field()
  3. 6.3 filefield.module \filefield_field()

Implementation of CCK's hook_field().

File

./filefield.module, line 274

Code

function filefield_field($op, $node, $field, &$items, $teaser, $page) {
  $field_name = $field['field_name'];
  switch ($op) {

    // Called after content.module loads default data.
    case 'load':
      if (empty($items)) {
        return array();
      }
      foreach ($items as $delta => $item) {

        // Despite hook_content_is_empty(), CCK still doesn't filter out
        // empty items from $op = 'load', so we need to do that ourselves.
        if (empty($item['fid']) || !($file = field_file_load($item['fid']))) {
          unset($items[$delta]);
        }
        else {
          $items[$delta] = array_merge($item, $file);
        }
      }
      $items = array_values($items);

      // compact deltas
      return array(
        $field_name => $items,
      );
    case 'insert':
    case 'update':
      foreach ($items as $delta => $item) {
        $items[$delta] = field_file_save($node, $item);

        // Remove items from the array if they have been deleted.
        if (empty($items[$delta])) {
          unset($items[$delta]);
        }
      }
      $items = array_values($items);

      // compact deltas
      break;
    case 'presave':

      // Extract previous (permanent) files from the items array that have been
      // deleted or replaced, so that insert/update can remove them properly.
      foreach ($items as $delta => $item) {
        if (!empty($item['replaced_file'])) {
          $items[] = $item['replaced_file'];
        }
      }
      break;
    case 'delete revision':
      foreach ($items as $delta => $item) {

        // For hook_file($op = 'references'), remember that this is being deleted.
        $item['field_name'] = $field['field_name'];
        if (field_file_delete($item)) {
          unset($items[$delta]);
        }
      }
      $items = array_values($items);

      // compact deltas
      break;
    case 'delete':
      foreach ($items as $delta => $item) {

        // For hook_file($op = 'references'), remember that this is being deleted.
        $item['field_name'] = $field['field_name'];
        field_file_delete($item);
      }
      break;
    case 'sanitize':
      foreach ($items as $delta => $item) {

        // Cleanup $items during node preview.
        if (empty($item['fid']) || !empty($item['delete'])) {
          unset($items[$delta]);
        }
        else {

          // Load the complete file if a filepath is not available.
          if (!empty($item['fid']) && empty($item['filepath'])) {
            $items[$delta] = array_merge($item, field_file_load($item['fid']));
          }

          // Add nid so formatters can create a link to the node.
          $items[$delta]['nid'] = $node->nid;
        }
      }
      break;
  }
}