Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_entity_field_access().
- *
- * {@inheritdoc}
- *
- * @todo Move this to a form controller so we can hide the field if it has no
- * options available to it?
- */
- function group_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
- // Can't retrieve an entity from an empty item list.
- if (!isset($items)) {
- return AccessResult::neutral();
- }
- // We only care about the group_roles field when on a form.
- if ($field_definition->getName() != 'group_roles' || $operation !== 'edit') {
- return AccessResult::neutral();
- }
- **// We only care if it is attached to a group content entity.
- if ($items->getEntity()->getEntityTypeId() != 'group_content') {
- return AccessResult::neutral();
- }**
- /** @var DrupalgroupEntityGroupContentInterface $group_content */
- $group_content = $items->getEntity();
- // We only care if the group content entity is a group membership.
- if ($group_content->getContentPlugin()->getPluginId() != 'group_membership') {
- return AccessResult::neutral();
- }
- // Now that we know we're dealing with a group_roles field on a group
- // membership form, we need to check whether the group membership belongs to a
- // group yet. If not, we can't check for access and should always hide it.
- if (!$group = $group_content->getGroup()) {
- return AccessResult::forbidden();
- }
- // Only group administrators should be able to change the membership roles.
- return AccessResult::forbiddenIf(!$group->hasPermission('administer members', $account));
- }
- // We only care about the the my_test_field field.
- if ($field_definition->getName() != 'my_test_field'
- if ($items->getEntity()->getEntityTypeId() == 'group_content')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement