6

I am using JM3.4. I added some customized user groups. And I added a field let user select user group when registration. I am going to hack following code:

// Get the default new user group, Registered if not specified.
$system = $params->get('new_usertype', 2);

$this->data->groups[] = $system;

I am going to check what user selected and override the system configured group. So I added following code between previous two lines of code:

if (!strcmp($this->data->role, "group0"))
{
    $system = 10;
}
elseif (!strcmp($this->data->role, "group1"))
{
    $system = 11;
}

My question is how can I not hardcoding group id here, instead of 10,11, it should be something like: getGroupID('group0'), getGroupID('group1')

4 Answers 4

4

I'm not sure if Joomla has a better way to do this, but you can create your own function and query the database:

function getGroupId($groupName){
    $db = JFactory::getDBO();
    $db->setQuery($db->getQuery(true)
        ->select('*')
        ->from("#__usergroups")
    );
    $groups = $db->loadRowList();
    foreach ($groups as $group) {
        if ($group[4] == $groupName) // $group[4] holds the name of current group
            return $group[0];        // $group[0] holds group ID
    }
    return false; // return false if group name not found
}

Then use the following to get the ID of a group based on the name:

echo (getGroupId("Administrator"));

This will echo the ID of the group called "Administrator", if it exists.

$groups will look something like this:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 0
            [2] => 1
            [3] => 22
            [4] => Public
        )

    [1] => Array
        (
            [0] => 2
            [1] => 1
            [2] => 8
            [3] => 19
            [4] => Registered
        )
...

Additional info

This might not help in your case, but JUserHelper has a method called getUserGroups (https://api.joomla.org/cms-3/classes/JUserHelper.html#method_getUserGroups) that will list all groups a user belongs to. Usage:

getUserGroups(integer $userId) : array
1
  • 1
    It is better practice to perform the filtration as early as possible to enjoy better performance and use less memory. Probably more stable/readable in the long term to generate an associative result set (in case columns get shifted around by core developers). You only need the group id after including the where() call, so loadResult() is best. 3v4l.org/e3gTP Commented Nov 13, 2019 at 12:58
2

Please avoid framework changes. This will lead you to system/functionality break after update. Create a User type plugin and work in it on event called onUserAfterSave.That way you will be update-safe. Add proper parameter fields(group0 and group1) to plugin XML file. Plugins have their own configuration so you cant get those parameters every time you need them and change them in UI.

Here is a complete guide to plugins events:

Here you can find plugin example:

Here is a field type you want to use as parameter in plugin configuration

2

You can create your own function that you can call to get the group id as :

public function getGroupId($groupName)
{
   $db = JFactory::getDbo();
   $select = "select id from #__usergroups where title='".$groupName."'";
   $db->setQuery($select);
   $db->query();
   $data = $db->loadObject();

   $groupId = $data->id;

   if(empty($groupId))
   $groupId = 2;

   return $groupId;
}

then call it as:

$system = $this->getGroupId($this->data->role);
1
  • Use loadResult() when intending to take a single value from a result set. 3v4l.org/vSFrG Commented Nov 13, 2019 at 12:48
0

Since Joomla 3.6.3 (with the addition of UserGroupsHelper class):

function getGroupId($groupName){
    foreach (JHelperUsergroups::getInstance()->getAll() as $group) {
        if ($group->title == $groupName) {
            return $group->id;
        }
    }
}

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.