You are here

function file_entity_file_update in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.file.inc \file_entity_file_update()
  2. 7 file_entity.module \file_entity_file_update()

Implements hook_file_update().

File

./file_entity.file.inc, line 80
File hooks implemented by the File entity module.

Code

function file_entity_file_update($file) {

  // Ensure field data is saved since file_save() does not in Drupal 7.
  field_attach_update('file', $file);

  // Save file metadata.
  db_delete('file_metadata')
    ->condition('fid', $file->fid)
    ->execute();
  if (!empty($file->metadata)) {
    $query = db_insert('file_metadata')
      ->fields(array(
      'fid',
      'name',
      'value',
    ));
    foreach ($file->metadata as $name => $value) {
      $query
        ->values(array(
        'fid' => $file->fid,
        'name' => $name,
        'value' => serialize($value),
      ));
    }
    $query
      ->execute();
  }
  if (module_exists('image') && file_entity_file_get_mimetype_type($file) == 'image' && $file->filesize && isset($file->original)) {
    if (!isset($file->metadata)) {
      $file->metadata = array();
    }
    if (!isset($file->original->metadata)) {
      if (!is_object($file->original)) {
        $file->original = new stdClass();
      }
      $file->original->metadata = array();
    }

    // If the file has changed dimensions or a new file has been uploaded,
    // update any image field reference to this file and flush image style
    // derivatives.
    $file->metadata += array(
      'width' => NULL,
      'height' => NULL,
    );
    $file->original->metadata += array(
      'width' => NULL,
      'height' => NULL,
    );
    if ($file->filesize != $file->original->filesize || $file->metadata['width'] != $file->original->metadata['width'] || $file->metadata['height'] != $file->original->metadata['height']) {
      _file_entity_update_image_field_dimensions($file);
    }

    // Flush image style derivatives whenever an image is replaced.
    if (empty($file->file_entity_skip_image_flush) && file_entity_has_file_changed($file)) {
      image_path_flush($file->uri);
    }
  }

  // Clear any related field caches.
  file_entity_invalidate_field_caches($file);
}