The "group_content_menu" entity with the ID "3" cannot have a URI as it does not have a group relationship in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete()

Created on 30 May 2025, 8 days ago

Hello,

I'm aware of some previous work to resolve this (or a similar) issue but at the moment it seems like it hasn't gone away, at least for me.

I'm having a bit of an issue with actually being able to delete group menus. Just trying to delete the group is becoming a bit of a problem because of the dependency with group_content_menu. I've applied the patch from https://www.drupal.org/node/3508929 but the error I get when I try to delete the group_content_menu relationship or the group_content_menu itself is:

The "group_content_menu" entity with the ID "3" cannot have a URI as it does not have a group relationship in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 763 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Now I believe this has some relation to the redirect module and I'm aware there was a patch that supposedly resolved that issue - I'm on the latest version of redirect, however it just ain't working.

This is the code I've been working with to test it:

    $group = Group::load(3);
    kint($group);

    $relationships = $group->getRelationships('group_content_menu:subsite_menu');
    $relatedEntities = $group->getRelatedEntities('group_content_menu:subsite_menu');

    kint($relationships);
    kint($relatedEntities);

    foreach ($relatedEntities as $relatedEntity) {
      $relatedEntity->delete();
    }

    foreach ($relationships as $relationship) {
      $relationship->delete();
    }

I get the same error on either $relatedEntity->delete() or $relationship->delete() - if I comment out one section of code or the other.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇬🇧United Kingdom welly

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

Comments & Activities

  • Issue created by @welly
  • 🇮🇳India abhishek@kumar

    Option A: Ensure Group Relationship Exists Before Deletion

    foreach ($relatedEntities as $relatedEntity) {
      if ($relatedEntity->hasField('group_id') && !$relatedEntity->get('group_id')->isEmpty()) {
        $relatedEntity->delete();
      }
    }
    

    Option B: Implement hook_entity_predelete()

    /**
     * Implements hook_entity_predelete().
     */
    function YOURMODULE_entity_predelete(EntityInterface $entity) {
      if ($entity->getEntityTypeId() === 'group_content_menu') {
        // Ensure the entity has a valid group relationship
        if (!$entity->hasField('group_id') || $entity->get('group_id')->isEmpty()) {
          // Temporarily add a dummy group relationship if needed
          $entity->set('group_id', 0);
        }
      }
    }
    

    Option C: Patch Group Content Menu Module

    The module should properly handle cases where group relationships are missing, either by:

    1. Preventing such entities from being created
    2. Providing proper fallback behavior during deletion
  • 🇬🇧United Kingdom welly

    Thanks for this, however I'm not sure that's quite right. The entity type doesn't have a group_id field because the relationship to the group is provided by the group relationship entity. It wouldn't be expected for the related entity to have a group_id field. I suspect ChatGPT is hallucinating in this instance ;-)

Production build 0.71.5 2024