2010-08-23 14:53:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Hooks for file module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Control download access to files.
|
|
|
|
*
|
2012-08-21 19:40:43 +00:00
|
|
|
* The hook is typically implemented to limit access based on the entity that
|
|
|
|
* references the file; for example, only users with access to a node should be
|
|
|
|
* allowed to download files attached to that node.
|
2010-08-23 14:53:50 +00:00
|
|
|
*
|
2012-02-28 18:32:20 +00:00
|
|
|
* @param $field
|
|
|
|
* The field to which the file belongs.
|
2012-08-21 15:19:09 +00:00
|
|
|
* @param Drupal\entity\EntityInterface $entity
|
2012-08-21 19:40:43 +00:00
|
|
|
* The entity which references the file.
|
2012-08-21 15:19:09 +00:00
|
|
|
* @param Drupal\Core\File\File $file
|
|
|
|
* The file entity that is being requested.
|
2010-08-23 14:53:50 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* TRUE is access should be allowed by this entity or FALSE if denied. Note
|
|
|
|
* that denial may be overridden by another entity controller, making this
|
|
|
|
* grant permissive rather than restrictive.
|
|
|
|
*
|
|
|
|
* @see hook_field_access().
|
|
|
|
*/
|
2012-08-21 15:19:09 +00:00
|
|
|
function hook_file_download_access($field, Drupal\entity\EntityInterface $entity, Drupal\Core\File\File $file) {
|
|
|
|
if ($entity->entityType() == 'node') {
|
2010-08-23 14:53:50 +00:00
|
|
|
return node_access('view', $entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter the access rules applied to a file download.
|
|
|
|
*
|
|
|
|
* Entities that implement file management set the access rules for their
|
|
|
|
* individual files. Module may use this hook to create custom access rules
|
|
|
|
* for file downloads.
|
|
|
|
*
|
|
|
|
* @see hook_file_download_access().
|
|
|
|
*
|
2011-05-08 19:50:38 +00:00
|
|
|
* @param $grants
|
2010-08-23 14:53:50 +00:00
|
|
|
* An array of grants gathered by hook_file_download_access(). The array is
|
|
|
|
* keyed by the module that defines the entity type's access control; the
|
|
|
|
* values are Boolean grant responses for each module.
|
2012-08-21 15:19:09 +00:00
|
|
|
* @param array $context
|
|
|
|
* An associative array containing the following key-value pairs:
|
2012-08-21 19:40:43 +00:00
|
|
|
* - field: The field to which the file belongs.
|
|
|
|
* - entity: The entity which references the file.
|
2012-08-21 15:19:09 +00:00
|
|
|
* - file: The file entity that is being requested.
|
2010-08-23 14:53:50 +00:00
|
|
|
*
|
2012-08-21 15:19:09 +00:00
|
|
|
* @see hook_file_download_access().
|
2010-08-23 14:53:50 +00:00
|
|
|
*/
|
2012-08-21 15:19:09 +00:00
|
|
|
function hook_file_download_access_alter(&$grants, $context) {
|
2010-08-23 14:53:50 +00:00
|
|
|
// For our example module, we always enforce the rules set by node module.
|
|
|
|
if (isset($grants['node'])) {
|
|
|
|
$grants = array('node' => $grants['node']);
|
|
|
|
}
|
|
|
|
}
|