Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get User Group Names in Joomla 2.5

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?

like image 966
nsimon Avatar asked May 06 '12 04:05

nsimon


1 Answers

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.

like image 176
Andrew Bucklin Avatar answered Sep 24 '22 23:09

Andrew Bucklin