1

This seems like it should be an easy question.

I'm writing a Joomla plugin which needs to loop through all subgroups of a specified user group.

But I can't find a Joomla API call to make that will load all the subgroups of a group.

Is there an API call that can do this, or will I need to write my own? It seems like the sort of thing that ought to exist?

Many thanks.

1

Groups are stored in a nested structure, implementing a Modified Preorder Tree Traversal.

To be brief, you can write a query to retrieve all childs under a parent, within a range n > parent-lft and n < parent-rgt.

0

I never did find a standard Joomla way of doing this, so I rolled my own function. For the benefit of anyone else wanting to do the same, here it is:

private function subGroupsOfConfigTopGroup($topGroup)
{
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select($db->quoteName('id'));
    $query->from($db->quoteName('#__usergroups'));
    $query->where($db->quoteName('parent_id').'='.(int)$topGroup);

    $db->setQuery($query);
    return $db->loadColumn();
}

This function takes a groupID as an argument and returns an array of groupsIDs that are direct subgroups of the original group.

Note: It does not check any further down the group tree. Joomla does allow heavily nested group structures but in my case I know that there won't be any further nested groups below this level so I'm saving processing time by not checking for them. If you're planning to use this function, please be aware of this and compensate for it if necessary.

I hope someone out there finds this helpful. And I'm still interested; if anyone does know of a standard Joomla way of doing this, please do post an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.