Group relationship to media not removed on group deletion

Created on 19 March 2025, 27 days ago

Problem/Motivation

In issue 3336280 πŸ› avoid fatal error when group argument to GroupAccessResult:: allowedIfHasGroupPermissions() is NULL Needs review a bug was reported that a fatal error was thrown in cases where a view tried to load content for a group that had been deleted.

TypeError: Drupal\group\Access\GroupAccessResult::
allowedIfHasGroupPermissions(): Argument #1 ($group) must be of type 
Drupal\group\Entity\GroupInterface, null given, called in 
modules/contrib/group/src/Plugin/GroupContentAccessControlHandler.php on line 145 in 
Drupal\group\Access\GroupAccessResult::allowedIfHasGroupPermissions() 
(line 57 of modules/contrib/group/src/Access/GroupAccessResult.php).

There are at least some reports that relationships were not deleted as part of the preDelete() function in src/Entity/Group.php. After some pretty thorough testing where I see the same error, I'm pretty sure the issue is not content or membership relationships, but I can reproduce the issue every time with media.

The relationships in group_relationship_field_data are not removed.

Steps to reproduce

  1. Install group and group media.
  2. Create a group type where media can be added
  3. Create a group of this type.
  4. Add media to the group.
  5. Delete the group without removing it from the media.
  6. Error should occur when you visit a view like /admin/content/media or a content view where the content displays the media that belongs to a group that has been deleted.
  7. Proposed resolution

    When I look through src/Entity/Group.php, I'm not immediately seeing why group media would be treated differently than group content or group memberships. Perhaps something is overriding it in the groupmedia module?

    Remaining tasks

    TBD

    User interface changes

    Shouldn't be any.

    API changes

    TBD

    Data model changes

    None.

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States joshuami Portland, OR

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @joshuami
  • πŸ‡§πŸ‡ͺBelgium lobsterr

    I tried to reproduce, without success.

    1) When we remove the group it calls the next code:

      public static function preDelete(EntityStorageInterface $storage, array $entities) {
        // Remove all relationships from these groups as well.
        foreach ($entities as $group) {
          assert($group instanceof GroupInterface);
          foreach ($group->getRelationships() as $relationship) {
            $relationship->delete();
          }
        }
      }
    

    It will remove all group relationships. What happens on my side and I see that the data also removed from group_relationship_field_data.
    Also I noticed that GroupContentAccessControlHandler, it looks like you are using Group 2.0 and groupmedia 3.0. Right ?

  • πŸ‡§πŸ‡ͺBelgium lobsterr
  • πŸ‡§πŸ‡ͺBelgium lobsterr

    I checked also with Group 2.0 and Group media 3.0 and it works as expected

  • πŸ‡ΊπŸ‡ΈUnited States joshuami Portland, OR

    I'm seeing the issue with Group 3.3.4 and Group Media 4.0.3. (Should have included that in the original I.S.)

    I would have expected preDelete to remove the media relationships as well, but it is not for this site. Let me do some additional testing and get back to you.

  • πŸ‡ΊπŸ‡ΈUnited States joshuami Portland, OR

    Ah, my bad. I copied the error from the related issue. I'm updating the I.S. to reflect the actual error I saw:

    TypeError: Drupal\group\Access\GroupAccessResult::allowedIfHasGroupPermissions(): Argument #1 ($group) must be of type Drupal\group\Entity\GroupInterface, null given, called in /var/www/html/docroot/modules/contrib/group/src/Plugin/Group/RelationHandlerDefault/AccessControl.php on line 190 in Drupal\group\Access\GroupAccessResult::allowedIfHasGroupPermissions() (line 57 of modules/contrib/group/src/Access/GroupAccessResult.php).
    
  • πŸ‡ΊπŸ‡ΈUnited States joshuami Portland, OR

    After a little more testing on a standard profile install of Drupal ^10 with Group ^3 and Group Media ^4, I'm not able to reproduce the issue. That likely means there is either a combination of permissions settings or perhaps some custom code.

    Our standard group type doesn't give the "Admin" role every permission. We rely on a site wide role to be able to do full group management. Individual group admins can manage members, but we don't want them adding or editing relationships outside of the content forms so delete any existing relationships is disabled for all entity types so they won't see that UI.

    We also have a custom module that does some syncing between relationships and a group entity reference field for more performant queries.

    Perhaps one of those choices is creating an edge case where media is not properly deleted on preDelete.

    Sorry for the noise, but perhaps this will help someone else running into the error because a relationship is not properly deleted.

Production build 0.71.5 2024