2012-03-15

Joomla 2.5 Add Field to Register Form

I highly recommend that you create a backup of your site before tampering with these files in the Joomla core!

In fact, I recommend that you go to my later post and add fields to your user forms the right way:  By creating an entirely separate user profile plugin.
Don't worry, I already did all the heavy lifting...


::WARNING::
Unfortunately, the method described below will be overwritten every time you update Joomla.  Apologies to anyone who used it without knowing this; I am learning as I go. Good news is that I went ahead and created an entire plugin (based on Joomla's stock profile plugin) called "Profile 7" to handle all this.  It will not be overwritten on updates.  I posted the whole thing on a later blog post.  So go there and use that.  I'm leaving this post up for reference and because it describes the process, which is important if you want to make your own custom fields.

UPDATE: APRIL 12, 2012:  The code below is tested for Joomla 2.5.4.
If you are using 2.5.3 or earlier version of Joomla and/or the profile plugin included with those versions, the "edit profile" page will not show any added fields.    In fact, it actually doesn't even show all of the fields that are included.  The register page does show the fields and the back end shows the fields.  It is only the edit profile page where they are not shown. One really messed up fact is that you can set a field to required on the administration panel and it will not show on the edit profile page, but when a user hits the submit button they will get an error telling them that field is required!  Thankfully, the Joomla team fixed this in 2.5.4.  You may run into issues if you try to implement this code in an earlier version.  I recommend you go ahead and backup your site and update!  Ok, back to the goods...

=======================================================================

This article from Joomla.org's official documentation was helpful, but it didn't tell this noob everything he needed to know.  All I needed to do was add a button, not create a whole new profile plugin.  However, the profile plugin that is included with Joomla has very similar code to the one in the article, so it was a nice frame of reference.  You will need FileZilla and full access to the administrator files on the server to make these changes.

The following are the files that will need changing:

/administrator/language/en-GB/en-GB.plg_user_profile.ini
/administrator/language/en-GB/en-GB.plg_user_profile.sys.ini
/plugins/user/profile/profile.xml
/plugins/user/profile/profile.php
/plugins/user/profile/profiles/profile.xml

Specifically, what I will be describing is adding a set of radio buttons to the register and profile edit forms so the user can choose to sign up for email updates.  Since Joomla 2.5 has a User Manager that allows you to create groups and sub-groups and manage them as well as mass send an email to any said group, I was able to make the profile plugin "talk to" Joomla and tell it to assign the user to a "subscriber" group which I create in the User Manager.

The first thing to do is to go into the Joomla User Manager.  Select the User Groups tab.  Select "New".
Enter the group title "subscriber".  Click the drop down arrow and select "registered" to make the new subscriber group a direct sub-group of "registered".  Now go to Users/Groups through the drop down menu.  Here you will see all of the groups listed in a hierarchical tree structure.  Find the new "subscriber" group.  In the last column on the right will be it's ID number.  In my case this number is 9.  BEWARE:  This is important, because this number is actually used in the code below to assign the user to the subscriber group.  If your group has a different number, then you will need to change this in the code below! 

The toughest part of this, for me, was tracking down those .ini files.  I had no idea that they would be filed under the administrator/language folder, but it makes perfect sense, because the files that we are changing there contain strings with labels, names and/or descriptions.  This allows support for multiple languages should this be necessary.

So I fired up FileZilla and pulled up the files for editing.

To the /plugins/user/profile/profile.xml right beneath the "dob" code I added:


<field
name="register-require_subscribe"
type="list"


label="PLG_USER_PROFILE_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

That will add something like this on the register page on the front end:


It is also referenced to create the field in the registration configuration form in the administrator back end.

To the /plugins/user/profile/profile.xml right beneath the "dob" code I added:

<field
name="profile-require_subscribe"
type="list"


label="PLG_USER_PROFILE_FIELD_SUBSCRIBE_LABEL"
description="PLG_USER_PROFILE_FIELD_SUBSCRIBE_DESC">
<option value="2">JOPTION_REQUIRED</option>
<option value="1">JOPTION_OPTIONAL</option>
<option value="0">JDISABLED</option>
</field>

Which creates this on the "edit profile" page on the front end:


It is also referenced to create the field in the profile configuration form in the administrator back end.

To the /plugins/user/profile/profiles/profile.xml I added (again right below the "tos" code):


<field name="subscribe" type="radio" default="0" label="PLG_USER_PROFILE_FIELD_SUBSCRIBE_LABEL" description="PLG_USER_PROFILE_FIELD_SUBSCRIBE_DESC"> <option value="0">PLG_USER_PROFILE_OPTION_NO</option> <option value="1">PLG_USER_PROFILE_OPTION_YES</option> </field>

Which will help (along with the rest of the code) to give you this...

...when users simply view their profiles.

For the /plugins/user/profile/profile.php file, find this bit of code:

// Add the registration fields to the form.
JForm::addFormPath(dirname(__FILE__).'/profiles');
$form->loadFile('profile', false);

$fields = array(
'address1',
'address2',
'city',
'region',
'country',
'postal_code',
'phone',
'website',
'favoritebook',
'aboutme',
'tos',
'dob',
'subscribe',
);

When you find it, it won't have "'subscribe'," at the bottom.  Add it.

To the bottom of the /administrator/language/en-GB/en-GB.plg_user_profile.ini file I added:

PLG_USER_PROFILE_FIELD_SUBSCRIBE_DESC="Subscribe to get emails about updates"
PLG_USER_PROFILE_FIELD_SUBSCRIBE_LABEL="Receive Update Emails"
PLG_USER_PROFILE_OPTION_YES="YES"

The /administrator/language/en-GB/en-GB.plg_user_profile.sys.ini actually doesn't need any changes.  (unless you want to rename it in Swahili or something).

Now I just need to figure out how to USE that "YES" selection.  What I want to do is make it so that when someone selects "YES" they are actually assigning themselves a member of the "Subscriber" group, an Assigned User Group that I created in the User Manager: User Groups tab in the Joomla Administration back end.  This way I will be able to send emails to that group in bulk whenever there is an update - easy peazy.  Since it IS possible for the users to go from "Public" to "Registered" I'm hoping this is also possible.  It would save a lot of hassle to have this automated.
===================

Added the "subscribe" version of the code below to the profile.php file

if (!JHtml::isRegistered('users.url')) {
JHtml::register('users.url', array(__CLASS__, 'url'));
}
if (!JHtml::isRegistered('users.calendar')) {
JHtml::register('users.calendar', array(__CLASS__, 'calendar'));
}
if (!JHtml::isRegistered('users.tos')) {
JHtml::register('users.tos', array(__CLASS__, 'tos'));
}
if (!JHtml::isRegistered('users.subscribe')) {
JHtml::register('users.subscribe', array(__CLASS__, 'subscribe'));
}

Also added this public static function later in the profile.php file (showing the tos version for example and for position information):

public static function tos($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}
public static function subscribe($value)
{
if ($value) {
return JText::_('JYES');
}
else {
return JText::_('JNO');
}
}

-------------------
Okay I've cobbled together this code from reading through forums all over the interwebs.  Time to see if it works...

//import the following at the top of profile.php
jimport( 'joomla.user.helper' );


jimport( 'joomla.user.user');


jimport( 'joomla.utilities.arrayhelper' );

-------------
put this inside the onUserAfterSave function in profile.php
Please note that the $groupID variable is set to 9 in the code below, but this could vary.  Check Users/Groups page in the Joomla Administrator to find the right value.
-------------

function onUserAfterSave($data, $isNew, $result, $error)
{
$userId = JArrayHelper::getValue($data, 'id', 0, 'int');
//
//BEGIN CUSTOM CODE
//
//put this inside the onUserAfterSave function in profile.php
// This code will check if the user clicked the radio button for email
// updates by checking the 'subscribe' boolean in the data
// then assign the user to that group (group 9) if true
$groupID = 9; //9 is the value for the subscriber group in the ACL
//DEBUG code that forces if statement to come up true:
//$subscribeFlag = 1;
if ($userId && (isset($data['profile']['subscribe'])))
{
$subscribeFlag = ($data['profile']['subscribe']);
if (!($subscribeFlag == 0)) 
{
try
{
JUserHelper::addUserToGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
else
{
try
{
JUserHelper::removeUserFromGroup($userId, $groupID);
}
catch (JException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
}
}
//END CUSTOM CODE
//

.......
(Note:  Originally I posted this code without the ability to select "no".  The removeUserFromGroup method of  the JUserHelper helps to accomplish this.)

Now, go to the Plugin Manager in Joomla open up User Profile and look under the Basic Options on the right.  Make sure Receive Update Emails is set to Required.

That should do it!  Now go to the register page on your site and register a pretend person.  Make sure you check the "Receive Update Emails" button!

Go to the User Manager in the Joomla Administration Back End, you should now see the new user you create has an additional group under the User Groups column:  Subscriber.

Later on, you can go to Mass Mail Users, in the User Menu, and simply select Subscriber and away goes the News Letter.  Good times.

I highly recommend making and keeping (in a safe memorable place) backups of all of the files to be altered in this process.

Here is a final copy of the entire profile.php file, complete with alterations:
profile.php


2 comments:

  1. I have read this post. collection of post is a nice one ..that am doing website designing company chennai india and website development company chennai india. That I will inform about your post to my friends and all the best for your future posts..

    ReplyDelete
  2. Good day,

    I read your post but I'm looking on the how to make custom joomla registration with ID field.. that ID will be field on ID of user. do you know something code will work on that thing?

    Regards,
    KennethRules

    ReplyDelete

Let me know what you think!