Same name and namespace in other branches
  1. 8.9.x core/modules/file/file.module \file_get_file_references()
  2. 9 core/modules/file/file.module \file_get_file_references()

Retrieves a list of references to a file.

Parameters

$file: A file object.

$field: (optional) A field array to be used for this check. If given, limits the reference check to the given field.

$age: (optional) A constant that specifies which references to count. Use FIELD_LOAD_REVISION to retrieve all references within all revisions or FIELD_LOAD_CURRENT to retrieve references only in the current revisions.

$field_type: (optional) The name of a field type. If given, limits the reference check to fields of the given type.

$check_access: (optional) A boolean that specifies whether the permissions of the current user should be checked when retrieving references. If FALSE, all references to the file are returned. If TRUE, only references from entities that the current user has access to are returned. Defaults to TRUE for backwards compatibility reasons, but FALSE is recommended for most situations.

Return value

An integer value.

Related topics

1 call to file_get_file_references()
file_file_download in modules/file/file.module
Implements hook_file_download().

File

modules/file/file.module, line 1102
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_get_file_references($file, $field = NULL, $age = FIELD_LOAD_REVISION, $field_type = 'file', $check_access = TRUE) {
  $references = drupal_static(__FUNCTION__, array());
  $fields = isset($field) ? array(
    $field['field_name'] => $field,
  ) : field_info_fields();
  foreach ($fields as $field_name => $file_field) {
    if ((empty($field_type) || $file_field['type'] == $field_type) && !isset($references[$field_name])) {

      // Get each time this file is used within a field.
      $query = new EntityFieldQuery();
      $query
        ->fieldCondition($file_field, 'fid', $file->fid)
        ->age($age);
      if (!$check_access) {

        // Neutralize the 'entity_field_access' query tag added by
        // field_sql_storage_field_storage_query().
        $query
          ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
      }
      $references[$field_name] = $query
        ->execute();
    }
  }
  return isset($field) ? $references[$field['field_name']] : array_filter($references);
}