field_permissions.api.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Field Permission module.
  5. */
  6. /**
  7. * Defines the owner of an entity.
  8. *
  9. * Because not all entities have uids, this hook allows other modules to specify
  10. * one.
  11. *
  12. * @param int $uid
  13. * The userid that will be checked against the current user's account->uid.
  14. * @param object $entity
  15. * The entity this field belongs to.
  16. */
  17. // @codingStandardsIgnoreStart
  18. function hook_field_permissions_userid_ENTITY_TYPE_alter(&$uid, $entity) {
  19. // This example always assigns user 15 as the owner of an entity.
  20. $uid = 15;
  21. }
  22. // @codingStandardsIgnoreEnd
  23. /**
  24. * Alter the permissions handled by field_permissions module.
  25. *
  26. * @param array $permissions
  27. * The $permissions array created by the Field permissions module.
  28. * @param string $field_label
  29. * The field name.
  30. */
  31. function hook_field_permissions_list_alter(&$permissions, $field_label) {
  32. $permissions += array(
  33. 'view own node preview' => array(
  34. 'label' => t('View own field on node preview'),
  35. 'title' => t('View own value for field %field on node preview', array('%field' => $field_label)),
  36. ),
  37. 'view node preview' => array(
  38. 'label' => t('View field on node preview'),
  39. 'title' => t("View anyone's value for field %field on node preview", array('%field' => $field_label)),
  40. ),
  41. );
  42. }
  43. /**
  44. * Hook invoked with custom field permissions.
  45. *
  46. * This hook can be used to revoke access to the field. If access is not
  47. * revoked, default access of the Field permissions module will apply.
  48. *
  49. * @param string $op
  50. * The operation to be performed. Possible values: 'edit', 'view'.
  51. * @param array $field
  52. * The field on which the operation is to be performed.
  53. * @param string $entity_type
  54. * The type of $entity; for example, 'node' or 'user'.
  55. * @param object $entity
  56. * (optional) The entity for the operation.
  57. * @param object $account
  58. * (optional) The account to check; if not given use currently logged in user.
  59. *
  60. * @return bool
  61. * FALSE if the operation is not allowed.
  62. *
  63. * @see field_permissions_field_access()
  64. * @see field_access()
  65. */
  66. function hook_field_permissions_custom_field_access($op, $field, $entity_type, $entity, $account) {
  67. if ($op == 'view' && $entity_type == 'node' && !empty($entity)) {
  68. // Check if user has access to view this field in any entity.
  69. if (!user_access('view node preview ' . $field['field_name'], $account)) {
  70. return FALSE;
  71. }
  72. // If the user has permission to view entities that they own, return TRUE if
  73. // they own this entity or FALSE if they don't.
  74. if (user_access('view own node preview ' . $field['field_name'], $account)) {
  75. return _field_permissions_entity_is_owned_by_account($entity, $account);
  76. }
  77. }
  78. return TRUE;
  79. }