Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /**
  2. * Implements hook_entity_field_access().
  3. *
  4. * {@inheritdoc}
  5. *
  6. * @todo Move this to a form controller so we can hide the field if it has no
  7. * options available to it?
  8. */
  9. function group_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  10. // Can't retrieve an entity from an empty item list.
  11. if (!isset($items)) {
  12. return AccessResult::neutral();
  13. }
  14.  
  15. // We only care about the group_roles field when on a form.
  16. if ($field_definition->getName() != 'group_roles' || $operation !== 'edit') {
  17. return AccessResult::neutral();
  18. }
  19.  
  20. **// We only care if it is attached to a group content entity.
  21. if ($items->getEntity()->getEntityTypeId() != 'group_content') {
  22. return AccessResult::neutral();
  23. }**
  24.  
  25. /** @var DrupalgroupEntityGroupContentInterface $group_content */
  26. $group_content = $items->getEntity();
  27.  
  28. // We only care if the group content entity is a group membership.
  29. if ($group_content->getContentPlugin()->getPluginId() != 'group_membership') {
  30. return AccessResult::neutral();
  31. }
  32.  
  33. // Now that we know we're dealing with a group_roles field on a group
  34. // membership form, we need to check whether the group membership belongs to a
  35. // group yet. If not, we can't check for access and should always hide it.
  36. if (!$group = $group_content->getGroup()) {
  37. return AccessResult::forbidden();
  38. }
  39.  
  40. // Only group administrators should be able to change the membership roles.
  41. return AccessResult::forbiddenIf(!$group->hasPermission('administer members', $account));
  42. }
  43.  
  44. // We only care about the the my_test_field field.
  45. if ($field_definition->getName() != 'my_test_field'
  46.  
  47. if ($items->getEntity()->getEntityTypeId() == 'group_content')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement