- 🇵🇹Portugal introfini
Here's the updated code for Group v3:
use Drupal\comment\CommentInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Session\AccountInterface; use Drupal\group\Entity\GroupInterface; use Drupal\group\Entity\GroupRelationship; /** * Implements hook_ENTITY_TYPE_access(). * * Allow group admins to edit or delete comments of that group content entities */ function hook_comment_access(CommentInterface $comment, $operation, AccountInterface $account) { // Allow group admins to edit and delete comments on group content entities. if (!in_array($operation, ['delete', 'update'])) { return AccessResult::neutral(); } $current_user = \Drupal::currentUser(); // Exit early if the current user has global permissions. if ( $current_user->hasPermission('bypass group access') || $current_user->hasPermission('administer comments') ) { return AccessResult::neutral(); } // Get the entity type manager service. $entity_type_manager = \Drupal::entityTypeManager(); // Load group relationships for the commented entity. $group_relationships = $entity_type_manager ->getStorage('group_relationship') ->loadByEntity($comment->getCommentedEntity()); if (empty($group_relationships)) { return AccessResult::neutral(); } $result = AccessResult::neutral(); /** @var \Drupal\group\GroupMembershipLoaderInterface $membership_loader */ $membership_loader = \Drupal::service('group.membership_loader'); /** @var \Drupal\group\Entity\GroupRelationship $relationship */ foreach ($group_relationships as $relationship) { // Get the group from the relationship $group = $relationship->getGroup(); if (!$group instanceof GroupInterface) { continue; } // Check if the user is a member of the group $membership = $membership_loader->load($group, $current_user); if ($membership) { // In Group v3, we check permissions directly through the membership if ($membership->hasPermission('administer group')) { $result = AccessResult::allowed(); break; } } } // Add cache contexts for user and group permissions return $result->addCacheContexts([ 'user.group_permissions', 'user.roles', ])->mergeCacheMaxAge(0); }