devel_node_access.api.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * @file
  4. * Hook provided by the Devel Node Access module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Explain your records in the {node_access} table.
  12. *
  13. * In order to help developers and administrators understand the forces
  14. * that control access to any given node, the DNA module provides the
  15. * Devel Node Access block, which lists all the grant records in the
  16. * {node_access} table for that node.
  17. *
  18. * However, every Node Access module is free in how it defines and uses the
  19. * 'realm' and 'gid' fields in its records in the {node_access} table, and
  20. * it's often difficult to interpret them. This hook passes each record
  21. * that DNA wants to display, and the owning module is expected to return
  22. * an explanation of that record.
  23. *
  24. * The explanation should not be localized (not be passed through t()), so
  25. * that administrators seeking help can present English explanations.
  26. *
  27. * @param $row
  28. * The record from the {node_access} table, as object. The member fields are:
  29. * nid, gid, realm, grant_view, grant_update, grant_delete.
  30. *
  31. * @return string|null
  32. * A string with a (short!) explanation of the given {node_access} row,
  33. * to be displayed in DNA's 'Devel Node Access' block. It will be displayed
  34. * as HTML; any variable parts must already be sanitized.
  35. *
  36. * @see hook_node_access_records()
  37. * @see devel_node_access_node_access_explain()
  38. *
  39. * @ingroup node_access
  40. */
  41. function hook_node_access_explain($row) {
  42. if ($row->realm == 'mymodule_myrealm') {
  43. if ($row->grant_view) {
  44. /** @var \Drupal\user\RoleInterface $role */
  45. $role = \Drupal\user\Entity\Role::load($row->gid);
  46. return t('Role %role may view this node.', array('%role' => $role->get('name')));
  47. }
  48. else {
  49. return 'No access.';
  50. }
  51. }
  52. return null;
  53. }
  54. /**
  55. * Acknowledge ownership of 'alien' grant records.
  56. *
  57. * Some node access modules store grant records directly into the {node_access}
  58. * table rather than returning them through hook_node_access_records(). This
  59. * practice is not recommended and DNA will flag all such records as 'alien'.
  60. *
  61. * If this is unavoidable, a module can confess to being the owner of these
  62. * grant records, so that DNA can properly attribute them.
  63. *
  64. * @see hook_node_access_records()
  65. *
  66. * @ingroup node_access
  67. */
  68. function hook_node_access_acknowledge($grant) {
  69. if ($grant['realm'] == 'mymodule_all' && $grant['nid'] == 0) {
  70. return TRUE;
  71. }
  72. return NULL;
  73. }
  74. /**
  75. * @} End of "addtogroup hooks".
  76. */