How do I programmatically retrieve the parent group from a subgroup?

Created on 25 July 2023, over 1 year ago
Updated 22 May 2024, 6 months ago

Problem/Motivation

I am looking for API methods to do the following:

1) Determine whether or not the current group type a subgroup
2) Retrieve the parent entity if the current group type is a subgroup

💬 Support request
Status

Active

Version

3.0

Component

Code

Created by

🇨🇦Canada Jaypan

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

Comments & Activities

  • Issue created by @Jaypan
  • 🇨🇦Canada Jaypan

    Well, this seems to work, though it's not through any kind of Subgroup API. I'm guessing there is probably a better solution through the Supgroup API, but for now I'm using this:

    $parent_relationships = GroupRelationship::loadByEntity($group);
      if (!empty($parent_relationships) && $parent_relationship = array_pop($parent_relationships)) {
        $parent = $parent_relationship->getGroup();
      }
    }
    
  • 🇮🇳India kssundar Bengaluru

    This works for me, thanks @jaypan

  • 🇳🇱Netherlands ricovandevin

    I have found that

    $parent_entity = $this->entityTypeManager->getHandler('group', 'subgroup')->getParent($child_entity);

    works. It has been quite a challenge to find out. :-)

  • 🇨🇦Canada Jaypan

    I have found that

    $parent_entity = $this->entityTypeManager->getHandler('group', 'subgroup')->getParent($child_entity);

    works. It has been quite a challenge to find out. :-)

    That works for me, thanks! That does seem a better method.

  • 🇦🇹Austria mvonfrie
    $parent_entity = $this->entityTypeManager->getHandler('group', 'subgroup')->getParent($child_entity);
    

    doesn't work for me. It throws MalformedLeafExceptions, at least for subgroups added programmatically.

  • 🇨🇦Canada Jaypan

    That may potentially indicate you haven't added the subgroup correctly when doing it programatically. I would start by doing a comparison of one added through the UI, and one added programatically, to make sure you haven't missed something.

  • 🇦🇹Austria mvonfrie

    @Jaypan, that sounds legit. More helpful would be a proper documentation with examples how to do things (adding, removing, ...) subgroups correctly via API.

  • 🇳🇱Netherlands Jan-E

    Simplified example of how I added lots of cases (subgroup) to projects (group and subgroup v.1):

    	$group_creator = $some_uid;
    	$group_creator_account = \Drupal\user\Entity\User::load($group_creator);
    	$group = \Drupal::entityTypeManager()->getStorage('group')->create(['type' => 'case']);
    	$group->setOwner($group_creator_account);
    	$group->set('label', $somelabel);
    	$group->set("status", 1);
    	$group->save();
    	$caseid = $group->id();
    	$pluginId = 'subgroup:case';
    	$project = \Drupal\group\Entity\Group::load($projectid);
    	$relation = $project->getContentByEntityId($pluginId, $caseid);
    	if (!$relation) {
    		$project->addContent($group, $pluginId);
    	}
    	$project->save();
    
Production build 0.71.5 2024