function video_field_update in Video 7.2
Same name and namespace in other branches
- 7 video.field.inc \video_field_update()
Implements hook_field_update().
@todo handle new revisions
File
- ./
video.field.inc, line 471 - Implement a video field, based on the file module's file field.
Code
function video_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
// The derived files are handled first, because when the original file is deleted first,
// the derived files can not be looked up anymore.
// Modification of file_field_update().
// That function can't be called because the logic for finding
// existing fids is not the same.
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
// Register the thumbnails in file_usage
$thumbnails = _video_field_get_all_thumbnails($field, $items);
if (!empty($thumbnails)) {
db_update('file_managed')
->fields(array(
'status' => FILE_STATUS_PERMANENT,
))
->condition('fid', array_keys($thumbnails), 'IN')
->execute();
}
// Register the thumbnails in file_usage
$converted = _video_field_get_all_converted($items);
$derivedfiles = array_merge($converted, $thumbnails);
// Build a display of the current FIDs.
$current_fids = array();
foreach ($derivedfiles as $item) {
$current_fids[] = $item['fid'];
}
// Create a bare-bones entity so that we can load its previous values.
$original = entity_create_stub_entity($entity_type, array(
$id,
$vid,
$bundle,
));
field_attach_load($entity_type, array(
$id => $original,
), FIELD_LOAD_CURRENT, array(
'field_id' => $field['id'],
));
// Compare the original field values with the ones that are being saved.
$original_fids = array();
if (!empty($original->{$field['field_name']}[$langcode])) {
$original_thumbs = _video_field_get_all_thumbnails($field, $original->{$field['field_name']}[$langcode], $entity_type, $id);
$original_converted = _video_field_get_all_converted($original->{$field['field_name']}[$langcode], $entity_type, $id);
$original_derivedfiles = array_merge($original_thumbs, $original_converted);
foreach ($original_derivedfiles as $original_derivedfile) {
$original_fids[] = $original_derivedfile['fid'];
if (isset($original_derivedfile['fid']) && !in_array($original_derivedfile['fid'], $current_fids)) {
// Decrement the file usage count by 1 and delete the file if possible.
file_field_delete_file($original_derivedfile, $field, $entity_type, $id);
}
}
}
// Add new usage entries for newly added files.
foreach ($derivedfiles as $item) {
if (!in_array($item['fid'], $original_fids)) {
$file = (object) $item;
file_usage_add($file, 'file', $entity_type, $id);
}
}
// Process the original file
file_field_update($entity_type, $entity, $field, $instance, $langcode, $items);
_video_field_file_autoconversion($entity_type, $entity, $field, $instance, $langcode, $items);
}