9

I'm writing a Joomla 2.5 component that I had been developing in Joomla 1.7. I have been using code like this:

$user = JFactory::getUser();
$groups = $user->get('groups');

The $groups array would contain a list of ids with the group name as the index. Joomla 2.5 seems to have scrapped this functionality. I have been unable to find out how to get the group names without directly querying the database. Is there any method for getting a list of the groups a user is a member of without having to resort to querying the database?

4 Answers 4

8

The code I generated below gets the names of all the groups the user is a part of and stores them in the variable $groupNames separated by line breaks:

foreach ($user->groups as $groupId => $value){
    $db = JFactory::getDbo();
    $db->setQuery(
        'SELECT `title`' .
        ' FROM `#__usergroups`' .
        ' WHERE `id` = '. (int) $groupId
    );
    $groupNames .= $db->loadResult();
    $groupNames .= '<br/>';
}
print $groupNames;

It technically queries the database but is done via the Joomla API. This is working well for me on Joomla 2.5.

1
  • 2
    It is not good idea to execute any database queries in loops.
    – SkipIntro
    Apr 18, 2019 at 14:13
4

Yes, this changed.

But what you should be using instead is:

JFactory::getUser()->getAuthorisedGroups();

or just getUserGroups

2
  • 7
    For all of these cases, I have found that the returned array is of the form: ([0] => '1', [1] => '2'). They do not contain the group names.
    – nsimon
    May 7, 2012 at 4:40
  • 1
    Sadly they can't contain the name because it is not constrained to be unique.
    – Elin
    Sep 22, 2013 at 19:14
1

Real snippet:

            $user = JFactory::getUser();
            $db = JFactory::getDBO();

    $db->setQuery($db->getQuery(true)
        ->select('*')
        ->from("#__usergroups")
    );
    $groups=$db->loadRowList();

            $userGroups = $user->groups;
            $return=array();

          foreach ($groups as $key=>$g){
            if (array_key_exists($g[0],$userGroups)) array_push($return,$g[4]);
          }

          $groups=$return;

      /////printing groupnames for current user/////////////
         print_r($groups);
7
  • 1. This answer is missing its educational explanation. 2. I don't understand why you wouldn't apply the filtration logic in the query; grabbing too much in the result set then cutting it down with php doesn't seem like professional-grade advice. Oct 15, 2020 at 3:44
  • The fact that it works is only the start of posting a good answer that will help researchers. This answer still needs editing when you have time. Oct 15, 2020 at 21:13
  • @mickmackusa, I agree that this is unoptimized in terms of database query, however, it is easier to debug when embedded in existing code regardless of the Joomla version. There are always few groups, but the table structure is different in different Joomla versions. Or does your task contain a large number of user groups that caused this discussion?
    – xmoonlight
    Oct 18, 2020 at 1:16
  • I do not need this solution personally. I am blowing my whistle that this answer is not using the most direct technique to generate the desired output. This is a free service that I provide which helps unknowing researchers to differentiate good answers from not-so-good answers because sometimes the vote tally tells lies. Oct 18, 2020 at 2:26
  • @mickmackusa, Agree, but this is a pretty flexible answer for the reasons I mentioned earlier. This is not your case for help. But, thanks for such comments. They are really helpful.
    – xmoonlight
    Oct 18, 2020 at 3:25
0

Here it is:

<?php

    $user =& JFactory::getUser();

    foreach ($user->groups as $key => $value){
        echo $key.'<br>';
    }

?>

This will print all the user group names to the screen. The user group names are the "keys" of the array $user->groups.

1
  • That prints the groups ids, not the names. Oct 27, 2016 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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