You are here

function file_get_file_references in Drupal 8

Same name and namespace in other branches
  1. 7 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

\Drupal\file\FileInterface $file: A file entity.

\Drupal\Core\Field\FieldDefinitionInterface|null $field: (optional) A field definition to be used for this check. If given, limits the reference check to the given field. Defaults to NULL.

int $age: (optional) A constant that specifies which references to count. Use EntityStorageInterface::FIELD_LOAD_REVISION (the default) to retrieve all references within all revisions or EntityStorageInterface::FIELD_LOAD_CURRENT to retrieve references only in the current revisions of all entities that have references to this file.

string $field_type: (optional) The name of a field type. If given, limits the reference check to fields of the given type. If both $field and $field_type are given but $field is not the same type as $field_type, an empty array will be returned. Defaults to 'file'.

Return value

array A multidimensional array. The keys are field_name, entity_type, entity_id and the value is an entity referencing this file.

Related topics

2 calls to file_get_file_references()
FileAccessControlHandler::getFileReferences in core/modules/file/src/FileAccessControlHandler.php
Wrapper for file_get_file_references().
file_file_download in core/modules/file/file.module
Implements hook_file_download().

File

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

Code

function file_get_file_references(FileInterface $file, FieldDefinitionInterface $field = NULL, $age = EntityStorageInterface::FIELD_LOAD_REVISION, $field_type = 'file') {
  $references =& drupal_static(__FUNCTION__, []);
  $field_columns =& drupal_static(__FUNCTION__ . ':field_columns', []);

  // Fill the static cache, disregard $field and $field_type for now.
  if (!isset($references[$file
    ->id()][$age])) {
    $references[$file
      ->id()][$age] = [];
    $usage_list = \Drupal::service('file.usage')
      ->listUsage($file);
    $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : [];
    foreach ($file_usage_list as $entity_type_id => $entity_ids) {
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entity_type_id)
        ->loadMultiple(array_keys($entity_ids));
      foreach ($entities as $entity) {
        $bundle = $entity
          ->bundle();

        // We need to find file fields for this entity type and bundle.
        if (!isset($file_fields[$entity_type_id][$bundle])) {
          $file_fields[$entity_type_id][$bundle] = [];

          // This contains the possible field names.
          foreach ($entity
            ->getFieldDefinitions() as $field_name => $field_definition) {

            // If this is the first time this field type is seen, check
            // whether it references files.
            if (!isset($field_columns[$field_definition
              ->getType()])) {
              $field_columns[$field_definition
                ->getType()] = file_field_find_file_reference_column($field_definition);
            }

            // If the field type does reference files then record it.
            if ($field_columns[$field_definition
              ->getType()]) {
              $file_fields[$entity_type_id][$bundle][$field_name] = $field_columns[$field_definition
                ->getType()];
            }
          }
        }
        foreach ($file_fields[$entity_type_id][$bundle] as $field_name => $field_column) {

          // Iterate over the field items to find the referenced file and field
          // name. This will fail if the usage checked is in a non-current
          // revision because field items are from the current
          // revision.
          // We also iterate over all translations because a file can be linked
          // to a language other than the default.
          foreach ($entity
            ->getTranslationLanguages() as $langcode => $language) {
            foreach ($entity
              ->getTranslation($langcode)
              ->get($field_name) as $item) {
              if ($file
                ->id() == $item->{$field_column}) {
                $references[$file
                  ->id()][$age][$field_name][$entity_type_id][$entity
                  ->id()] = $entity;
                break;
              }
            }
          }
        }
      }
    }
  }
  $return = $references[$file
    ->id()][$age];

  // Filter the static cache down to the requested entries. The usual static
  // cache is very small so this will be very fast.
  $entity_field_manager = \Drupal::service('entity_field.manager');
  if ($field || $field_type) {
    foreach ($return as $field_name => $data) {
      foreach (array_keys($data) as $entity_type_id) {
        $field_storage_definitions = $entity_field_manager
          ->getFieldStorageDefinitions($entity_type_id);
        $current_field = $field_storage_definitions[$field_name];
        if ($field_type && $current_field
          ->getType() != $field_type || $field && $field
          ->uuid() != $current_field
          ->uuid()) {
          unset($return[$field_name][$entity_type_id]);
        }
      }
    }
  }
  return $return;
}