How to create User Group Progamatically Topic is solved

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
cscotthosting
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Mon Jan 02, 2023 3:14 pm

How to create User Group Progamatically

Post by cscotthosting » Sun Mar 24, 2024 1:43 pm

Hello,
I have seen a bunch of examples in google searches and here on how to add a user to Joomla 5.
However, I am in need of adding the creation of a User Group when a user is created, progamatically.

Can someone assist with the code to create a User Group.

Thanks

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 25018
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: How to create User Group Progamatically

Post by pe7er » Sun Mar 24, 2024 4:27 pm

Welcome to Joomla forum!

In Joomla 5 you can probably create a new User Group with something like the code below:

Code: Select all

use Joomla\CMS\Factory;
use Joomla\Component\Users\Administrator\Model\GroupModel;

$group = [
    'id' => 0, 
    'title' => 'Your new User Group Name', 
    'parent_id' => 1
];

Factory::getApplication()->bootComponent('com_users');
$model = new GroupModel;

if (!$model->save($group)) {
    Factory::getApplication()->enqueueMessage($model->getError(), 'error');
    return false;
}
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

cscotthosting
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Mon Jan 02, 2023 3:14 pm

Re: How to create User Group Progamatically

Post by cscotthosting » Mon Mar 25, 2024 10:50 am

Worked like a charm, Thank You. Lastly, how do I get the id of the newly created group?
I tried {var:php9.id}, outside the php block, but that did not work. I have a Joomla Message that displays the new id for the user and the group...

The User {data:username} userid= {var:joomla_user7.id} and groupid = {var:php9.id} has been added.

cscotthosting
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Mon Jan 02, 2023 3:14 pm

Re: How to create User Group Progamatically

Post by cscotthosting » Wed Mar 27, 2024 9:16 am

Anyone?

Lastly, how do I get the id of the newly created group?
I tried {var:php9.id}, outside the php block, but that did not work. I have a Joomla Message that displays the new id for the user and the group...

The User {data:username} userid= {var:joomla_user7.id} and groupid = {var:php9.id} has been added.

MarkRS
Joomla! Explorer
Joomla! Explorer
Posts: 347
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: How to create User Group Progamatically

Post by MarkRS » Wed Mar 27, 2024 11:21 am

cscotthosting wrote:
Wed Mar 27, 2024 9:16 am
Anyone?

Lastly, how do I get the id of the newly created group?
I've not done this, but my guess would be

Code: Select all

$yourNewGroup->getTable()->getId();
It's a community, the more we all contribute, the better it will be.

cscotthosting
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Mon Jan 02, 2023 3:14 pm

Re: How to create User Group Progamatically

Post by cscotthosting » Wed Mar 27, 2024 12:44 pm

Thanks MarkRS

What is $yourNewGroup. Is that the new group name that was created or literally the text I use in my code?

MarkRS
Joomla! Explorer
Joomla! Explorer
Posts: 347
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: How to create User Group Progamatically

Post by MarkRS » Wed Mar 27, 2024 3:52 pm

Yes, poor reply of mine, sorry.

I mean the groupModel object.
It's a community, the more we all contribute, the better it will be.


Post Reply

Return to “Joomla! 5.x Coding”