- 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(); } }
- 🇳🇱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
MalformedLeafException
s, 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();