hook_file_references

Definition

hook_file_references($file)
drupal/modules/system/system.api.php, line 1115

Description

Report the number of times a file is referenced by a module.

This hook is called to determine if a files is in use. Multiple modules may be referencing the same file and to prevent one from deleting a file used by another this hook is called.

@see file_delete() @see upload_file_references()

Parameters

$file The file object being checked for references.

Return value

If the module uses this file return an array with the module name as the key and the value the number of times the file is used.

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

function hook_file_references($file) {
  // If upload.module is still using a file, do not let other modules delete it.
  $count = db_query('SELECT COUNT(*) FROM {upload} WHERE fid = :fid', array(':fid' => $file->fid))->fetchField();
  if ($count) {
    // Return the name of the module and how many references it has to the file.
    return array('upload' => $count);
  }
}