1 file.module file_get_file_references(File $file, $field = NULL, $age = FIELD_LOAD_REVISION, $field_type = 'file', $check_access = TRUE)

Retrieves a list of references to a file.

Parameters

File $file: A file entity.

$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

array: A nested array in the following format:

  • field_name => field_references

    • entity_type => type_references

      • id => reference

The first level key contains the field names. The second level key contains the entity type. The third level key contains entity ID.

See also

file_file_download()

file_file_predelete()

Related topics

File

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

Code

function file_get_file_references(File $file, $field = NULL, $age = FIELD_LOAD_REVISION, $field_type = 'file', $check_access = TRUE) {
  $references = backdrop_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);
}