profile.api.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by profile module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Control access to a user profile.
  12. *
  13. * Modules may implement this hook to control whether a user has access to
  14. * perform a certain operation on a profile.
  15. *
  16. * @param string $op
  17. * The operation being performed. One of 'view', 'edit' (being the same as
  18. * 'create' or 'update'), or 'delete'.
  19. * @param Drupal\profile\Entity\Profile $profile
  20. * A profile to check access for.
  21. * @param Drupal\user\Entity\User $account
  22. * The user performing the operation; the currently logged in user by default.
  23. *
  24. * @return bool
  25. * Either a Boolean or NULL:
  26. * - FALSE to explicitly deny access. If a module denies access, no other
  27. * module is able to grant access and access is denied.
  28. * - TRUE to grant access. Access is only granted if at least one module
  29. * grants access and no module denies access.
  30. * - NULL or nothing to not affect the operation. If no module explicitly
  31. * grants access, access is denied.
  32. */
  33. function hook_profile_access($op, Drupal\profile\Entity\Profile $profile, Drupal\user\Entity\User $account) {
  34. // Explicitly deny access for a 'secret' profile type.
  35. if ($profile->getType() == 'secret' && !\Drupal::currentUser()->hasPermission('custom permission')) {
  36. return FALSE;
  37. }
  38. // For profiles other than the default profile grant access.
  39. if ($profile->getType() != 'main' && \Drupal::currentUser()->hasPermission('custom permission')) {
  40. return TRUE;
  41. }
  42. // In other cases do not alter access.
  43. return NULL;
  44. }
  45. /**
  46. * @}
  47. */