|Joomla! 2.5 - Auto assign users in user group on registration|
|Παρασκευή, 22 Ιουνίου 2012 10:21|
Since Joomla! nowdays (1.6+) has a powerful ACL and user groups management, assigning users in specific usergroups during registration is the logical next step. While searching for a guide on how to do this, I only found people asking about it... and since I implemented it some time ago I thought I'd share my findings.
Let's say we're making a community site for music artists, which could include guitar players, singers, bass players and drum players.
Step 1: Create the user groups
In Joomla! admin, Users->Groups->Add new group
All of them, should have 'Registered' as the parent user group (it can get more complicated if you are using specific access levels but let's stick to registered for now).
While creating the user groups, take a note of their id (right column in the user groups list), we're going to need that later.
Done with the usergroups, lets move on.
Step 2: Create access levels [optional]
Since we have our user groups, we'll probably want to control our content and display Jimmy Hendrix for the guitarists, Jaco Pastorius for the bass players etc. Thats done through creating access levels, connecting user groups to these access levels and creating categories / content / modules that can be accessed through specific access levels. I won't get any deeper on this since it's out of this guide's scope and should be easy to implement anyway. Ofcourse, I'll be glad to answer any possible questions if i can.
Step 3: User - profile plugin
It's already there, you only need to enable it. It adds some extra fields during registration, which we are going to use for group assignment.
Go to Plugin manager and you'll find it under 'user' type. Open it, have at least one field enabled (required if you MUST assign all users in a group, else optional) and dont forget to enable the plugin, too.
Step 4: registration fields
What we need to do now is:
In order to do that, we need to edit a couple of files:
a. Registration form:
Somewhere among the other fields, include a hidden input, named "assign":
<field name="assign" type="hidden"
Assuming you have an input field the user must fill, named "address1" (this is the name of the first field in user-profile plugin - you can change it's label to whatever you want either by editing plugins/user/profile/profiles/profile.xml or by overriding PLG_USER_PROFILE_FIELD_ADDRESS1_LABEL in language manager) with his interest in music, which can be either guitar, singing, bass or drums, i would do something like this with jQuery:
There are much better ways to do that, ofcourse, depending on your specific setup. That's just to give you an idea, you should consider a lot more cases than i do in this example.
Now we only need to do one more thing...
Step 5: write user group in database
Since we post the group id during registration submission, we need to get this id somehow and write it. Thankfully, it's quite easy to do that but we need to edit one more file:
We need to add a group index with an assign value to the data array, so we change it to:
A note for keeping notes: sooner or later, the core files we edited will be overriden during a joomla update. Since these files are not views / templates and cant be overriden somehow (afaik) its very important to keep a note of the edits you made, so that you can make the same changes after an update made all your effort dissapear :D
If you have some feedback, alternate ways or anything you'd like to share please use the comments or alou [at] alou [dot] gr.