Commit e9890c3b authored by dopry's avatar dopry

update hook_file implementation to reflect modt recent core hook_file path with callbacks per $op.

add field_file_references($file, $field) to load references by field.
rename $item['filefield_delete'] to more descriptive $item['field_name'].
parent 84732f9a
......@@ -38,9 +38,9 @@ function field_file_load($fid, $reset = NULL) {
return array();
}
foreach (module_implements('file') as $module) {
$function = $module .'_file';
$function('load', $file);
foreach (module_implements('file_load') as $module) {
$function = $module .'_file_load';
$function($file);
}
// Cache the fully loaded file for later reusability.
......@@ -86,9 +86,9 @@ function field_file_save_upload($source, $validators = array(), $dest = FALSE, $
return 0;
}
// Let modules add additional properties to the yet barebone file object.
foreach (module_implements('file') as $module) {
$function = $module .'_file';
$function('insert', $file);
foreach (module_implements('file_insert') as $module) {
$function = $module .'_file_insert';
$function($file);
}
_field_file_cache($file); // cache the file in order to minimize load queries
return (array)$file;
......@@ -126,9 +126,9 @@ function field_file_save($node, &$file) {
}
// Let modules update their additional file properties too.
foreach (module_implements('file') as $module) {
$function = $module .'_file';
$function('update', $file);
foreach (module_implements('file_update') as $module) {
$function = $module .'_file_update';
$function($file);
}
_field_file_cache($file); // update the cache, in case the file has changed
......@@ -151,7 +151,7 @@ function field_file_delete($file, $force = FALSE) {
// If any module returns a value from the reference hook, the
// file will not be deleted from Drupal, but file_delete will
// return a populated array that tests as TRUE.
if (!$force && $references = module_invoke_all('file', 'references', $file)) {
if (!$force && $references = module_invoke_all('file_references', $file)) {
$references = array_filter($references); // only keep positive values
if (!empty($references)) {
return $references;
......@@ -159,7 +159,7 @@ function field_file_delete($file, $force = FALSE) {
}
// Let other modules clean up on delete.
module_invoke_all('file', 'delete', $file);
module_invoke_all('file_delete', $file);
// Make sure the file is deleted before removing its row from the
// database, so UIs can still find the file in the database.
......@@ -267,3 +267,21 @@ function field_file_strip_path($path) {
}
return $path;
}
/**
* return references to a file by a single field.
*/
function field_file_references($file, $field) {
$db_info = content_database_info($field);
$references += db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
));
if (isset($file->field_name) && $field['field_name'] == $file->field_name) {
--$references; // doesn't count as it's being deleted
}
return $references;
}
......@@ -154,28 +154,15 @@ function filefield_icon_url($file) {
* Implementation of hook_file().
* (Which is implemented by filefield/imagefield in Drupal 6 yet).
*/
function filefield_file($op, $file) {
switch ($op) {
// report references to prevent deletion of files associated to older revisions
// or by other fields.
case 'references':
$references = 0;
foreach(content_fields() as $field) {
if ($field['type'] != 'file') {
continue;
}
$db_info = content_database_info($field);
$references += db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
));
if (isset($file->filefield_delete) && $field['field_name'] == $file->filefield_delete) {
--$references; // doesn't count as it's being deleted
}
}
return array('filefield' => $references);
function filefield_file_references($op, $file) {
$references = 0;
foreach(content_fields() as $field) {
if ($field['type'] != 'file') {
continue;
}
$references += field_file_references($file, $field);
}
return array('filefield' => $references);
}
......@@ -332,7 +319,7 @@ function filefield_field($op, $node, $field, &$items, $teaser, $page) {
case 'delete revision':
foreach ($items as $delta => $item) {
// For hook_file($op = 'references'), remember that this is being deleted.
$item['filefield_delete'] = $field['field_name'];
$item['field_name'] = $field['field_name'];
if (field_file_delete($item)) {
unset($items[$delta]);
}
......@@ -343,7 +330,7 @@ function filefield_field($op, $node, $field, &$items, $teaser, $page) {
case 'delete':
foreach ($items as $delta => $item) {
// For hook_file($op = 'references'), remember that this is being deleted.
$item['filefield_delete'] = $field['field_name'];
$item['field_name'] = $field['field_name'];
field_file_delete($item);
}
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment