file_usage_add

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 file_usage_add(stdClass $file, $module, $type, $id, $count = 1)

Records that a module is using a file.

This usage information will be queried during file_delete() to ensure that a file is not in use before it is physically removed from disk.

Examples:

  • A module that associates files with nodes, so $type would be 'node' and $id would be the node's nid. Files for all revisions are stored within a single nid.
  • The User module associates an image with a user, so $type would be 'user' and the $id would be the user's uid.

Parameters

$file A file object.

$module The name of the module using the file.

$type The type of the object that contains the referenced file.

$id The unique, numeric ID of the object containing the referenced file.

$count (optional) The number of references to add to the object. Defaults to 1.

See also

file_usage_list()

file_usage_delete()

Related topics

▾ 9 functions call file_usage_add()

FileDeleteTest::testInUse in modules/simpletest/tests/file.test
Tries deleting a file that is in use.
FileUsageTest::testAddUsage in modules/simpletest/tests/file.test
Tests file_usage_add().
file_field_insert in modules/file/file.field.inc
Implements hook_field_insert().
file_field_update in modules/file/file.field.inc
Implements hook_field_update().
image_example_style_form_submit in examples/image_example/image_example.pages.inc
Form Builder; Display a form for uploading an image.
image_field_update_field in modules/image/image.module
Implements hook_field_update_field().
system_update_7061 in modules/system/system.install
Migrate upload.module data to the newly created file field.
user_save in modules/user/user.module
Save changes to a user account or add a new user.
user_update_7013 in modules/user/user.install
Add user module file usage entries.

Code

includes/file.inc, line 654

<?php
function file_usage_add(stdClass $file, $module, $type, $id, $count = 1) {
  db_merge('file_usage')
    ->key(array(
    'fid' => $file->fid, 
    'module' => $module, 
    'type' => $type, 
    'id' => $id,
  ))
    ->fields(array('count' => $count))
    ->expression('count', 'count + :count', array(':count' => $count))
    ->execute();
}
?>