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?

ekad
  • 14,436
  • 26
  • 44
  • 46
nsimon
  • 91
  • 1
  • 1
  • 5

4 Answers4

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.

Andrew Bucklin
  • 699
  • 8
  • 19
4

Yes, this changed.

But what you should be using instead is:

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

or just getUserGroups

Tom
  • 2,604
  • 11
  • 57
  • 96
  • 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 07 '12 at 04:40
  • 1
    Sadly they can't contain the name because it is not constrained to be unique. – Elin Sep 22 '13 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);
xmoonlight
  • 199
  • 4
  • 12
  • 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. – mickmackusa Oct 15 '20 at 03: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. – mickmackusa Oct 15 '20 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 '20 at 01: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. – mickmackusa Oct 18 '20 at 02: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 '20 at 03:25
  • You are already using `$user->groups`, just move the usage to the query. This improvement in no way diminishes the flexibility of your snippet. It only improves your snippet. Please edit your answer to optimize the filtration step at the earliest point and add an educational explanation as to how your advised code works and why you feel it is good advice. Refusing to do so will mean fewer researchers will be helped and/or researchers will be copy-pasting suboptimal code. – mickmackusa Oct 18 '20 at 04:10
  • @mickmackusa, Not very clear. Can you show what you want with the source code (short version)? – xmoonlight Oct 20 '20 at 21:13
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.

jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160