You are here

function filefield_field_sanitize in FileField 6.3

Implementation of CCK's hook_field($op = 'sanitize').

File

./filefield_field.inc, line 277
FileField CCK field hooks and callbacks.

Code

function filefield_field_sanitize($node, $field, &$items, $teaser, $page) {
  foreach ($items as $delta => $item) {

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

      // Check for default images at the widget level.
      // TODO: Provide an API to ImageField to do this itself?
      if (!empty($field['widget']['use_default_image']) && !empty($field['widget']['default_image']['filepath']) && $delta == 0) {
        $items[$delta] = $field['widget']['default_image'];
        $items[$delta]['default'] = TRUE;
      }
      else {
        $items[$delta] = NULL;
        continue;
      }
    }

    // Add nid so formatters can create a link to the node.
    $items[$delta]['nid'] = isset($node->nid) ? $node->nid : NULL;

    // Get the 'data' column stored by CCK into an array. This is necessary
    // for Views, which doesn't call the "load" $op and to fix an issue with
    // CCK double-serializing data.
    // See the FileField issue http://drupal.org/node/402860.
    // And the CCK issue http://drupal.org/node/407446.
    while (!empty($items[$delta]['data']) && is_string($items[$delta]['data'])) {
      $items[$delta]['data'] = unserialize($items[$delta]['data']);
    }

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

    // Verify the file exists on the server.
    if (!empty($item['filepath']) && !file_exists($item['filepath'])) {
      watchdog('filefield', 'FileField was trying to display the file %file, but it does not exist.', array(
        '%file' => $item['filepath'],
      ), WATCHDOG_WARNING);
    }
  }
}