file.api.php

Hooks for file module.

File

drupal-7.x/modules/file/file.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks for file module.
  5. */
  6. /**
  7. * Control download access to files.
  8. *
  9. * The hook is typically implemented to limit access based on the entity the
  10. * file is referenced, e.g., only users with access to a node should be allowed
  11. * to download files attached to that node.
  12. *
  13. * @param array $file_item
  14. * The array of information about the file to check access for.
  15. * @param $entity_type
  16. * The type of $entity; for example, 'node' or 'user'.
  17. * @param $entity
  18. * The $entity to which $file is referenced.
  19. *
  20. * @return
  21. * TRUE is access should be allowed by this entity or FALSE if denied. Note
  22. * that denial may be overridden by another entity controller, making this
  23. * grant permissive rather than restrictive.
  24. *
  25. * @see hook_field_access().
  26. */
  27. function hook_file_download_access($file_item, $entity_type, $entity) {
  28. if ($entity_type == 'node') {
  29. return node_access('view', $entity);
  30. }
  31. }
  32. /**
  33. * Alter the access rules applied to a file download.
  34. *
  35. * Entities that implement file management set the access rules for their
  36. * individual files. Module may use this hook to create custom access rules
  37. * for file downloads.
  38. *
  39. * @see hook_file_download_access().
  40. *
  41. * @param $grants
  42. * An array of grants gathered by hook_file_download_access(). The array is
  43. * keyed by the module that defines the entity type's access control; the
  44. * values are Boolean grant responses for each module.
  45. * @param array $file_item
  46. * The array of information about the file to alter access for.
  47. * @param $entity_type
  48. * The type of $entity; for example, 'node' or 'user'.
  49. * @param $entity
  50. * The $entity to which $file is referenced.
  51. */
  52. function hook_file_download_access_alter(&$grants, $file_item, $entity_type, $entity) {
  53. // For our example module, we always enforce the rules set by node module.
  54. if (isset($grants['node'])) {
  55. $grants = array('node' => $grants['node']);
  56. }
  57. }