Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0
Description: 

In Drupal 7 the hook_file_download_access() had to be implemented in order to determine access over the file being downloaded/viewed.

In Drupal 8 the \Drupal\file\FileAccessControlHandler has been introduced and replaces the hook. The access control handler now uses the referencing entity's access methods to check access. Therefore, it is no longer necessary to handle access for files unless some specific logic is needed.

To take control over file access, you can use standard entity access hooks now. You generally do this by specifying access = Drupal\mymodule\MyModuleAccessControlHandler in your entity annotation, replacing it with the path to your access control handler. Your access control handler should usually extend \Drupal\Core\Entity\EntityAccessControlHandler.

Be aware that Drupal will only automatically check access for files stored in fields — specifically, fields of type `'file'` (see FileFieldItemList::postSave()). When you save such a field, the File module adds tracking information for it using the file.usage service (see also: #1812240: File usage is pluggable).

Use the following hooks if you need to control file access to managed files that don't meet this requirement.

D7

function mymodule_file_download_access($field, EntityInterface $entity, FileInterface $file) {
  // do access stuff...
}

D8

function mymodule_file_access($entity, $operation, $account, $langcode) {
  if ($operation == 'download') {
    // do access stuff...
  }
}

or

// NOTE: You generally only need to do this if you want to control access to a file based on another entity that you don't
// control. For your own entities, see above on how to define an access control handler.
function mymodule_entity_access($entity, $operation, $account, $langcode) {
  if ($entity->getEntityTypeId() == 'file') {
    // do access stuff...
  }
}

Also, note that a new operation type called 'download' has been introduced.

Impacts: 
Module developers