Wednesday, May 2, 2012

how to add custom registration field in joomla extend joomla registration in com_users

Once in a life, everybody needs something extra and when it comes to Joomla, it is really easy as there are many extensions available for Joomla but when it comes to extend Joomla registration in frontend and managing the same way in backend then it gets difficult and I faced this when I was working today over a Joomla project so I thought to prepare a note for future use as well as to share with rest of the community. (may be there are similar solutions already but this is how I solved it.)

My example shows how to add an extra field called company in Joomla registration:

---------------------------------------------------------------------------
Step 1. Add DB field variable to user.php

Please open file as below:

YOUR_SITE/libraries/joomla/database/table/user.php

and declare the variable as below to connect with the relevant field in database table for users i.e. jos_users


---------------------------------------------------------------------------

/**
* Description
*
* @var string
*/

var $company = null;

---------------------------------------------------------------------------


Step 2. Add field in the DB

As you have added relational variable above to connect with DB so here is the SQL Query to run which will add an additional field in the users table so that your entries can be managed in DB:


ALTER TABLE `jos_users` ADD `company` VARCHAR( 100 ) NOT NULL AFTER `username`

Please note that I have given the position after username field but you can leave or can change it as you need.

---------------------------------------------------------------------------


Step 3. Edit Default.php template file for user to add extra field details during registration

Please open file as below:

YOUR_SITE/components/com_user/views/register/tmpl/default.php

Now, look for input fields in the form and add your own to display here. Following to my example to add extra field called “Company”, I am adding below field to give you an idea:


<!-- CUSTOM COMPANY INPUT STARTS -->
<tr>
<td height="40">
<label id="companymsg" for="company">
<?php echo JText::_( 'Company' ); ?>:
</label>
</td>
<td>
<input type="text" id="company" name="company" size="40" value="<?php echo $this->escape($this->user->get( 'company' ));?>" class="inputbox required validate-company" maxlength="25" /> *
</td>
</tr>
<!-- CUSTOM COMPANY INPUT ENDS -->

---------------------------------------------------------------------------


Basic work finishes here as if you just want this extra field information for registration purpose only but if you need complete customization then please read further…

Step 4. Edit form.php template file for user to edit profile with extra field

Please open below file:

YOUR_SITE/components/com_user/views/user/tmpl/form.php

Now, look for input fields in the form and add your own to display here. Following to my example, I am adding below field to give you an idea:


---------------------------------------------------------------------------


<!-- CUSTOM COMPANY INPUT STARTS -->
<tr>
<td height="40">
<label id="companymsg" for="company">
<?php echo JText::_( 'Company' ); ?>:
</label>
</td>
<td>
<input type="text" id="company" name="company" size="40" value="<?php echo $this->escape($this->user->get( 'company' ));?>" class="inputbox required validate-company" maxlength="25" />
</td>
</tr>
<!-- CUSTOM COMPANY INPUT ENDS -->

---------------------------------------------------------------------------

Here, front panel customization completes, if you want similar changes to appear in the admin panel too then please proceed further…

Step 5. Edit default.php to add extended registration columns in the table view

Please open below file:

YOUR_SITE/administrator/components/com_users/views/users/tmpl/default.php

Now, please look for table heading () block and add one column for your extended field like below:


<!-- CUSTOM COMPANY COLUMN STARTS -->
<th width="15%" class="title" >
<?php echo JHTML::_('grid.sort', 'Company', 'a.company', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
</th>
<!-- CUSTOM COMPANY COLUMN ENDS -->


In the same file, now please go to the TD section for the actual data to appear and add a section just in the same sequence of TH heading placed as below:


<!-- CUSTOM COMPANY COLUMN STARTS -->
<td>
<?php echo $row->company; ?>
</td>
<!-- CUSTOM COMPANY COLUMN ENDS -->

---------------------------------------------------------------------------


Step 6. Edit form.php template file for user to edit profile with extra field to handle user creation or edit by admin

Please open below file:

YOUR_SITE/administrator/components/com_users/views/user/tmpl/form.php

Please look for input fields and add one for your extended registration field like below:


<!-- CUSTOM COMPANY INPUT STARTS -->
<tr>
<td class="key">
<label id="companymsg" for="company">
<?php echo JText::_( 'Company' ); ?>:
</label>
</td>
<td>
<input type="text" id="company" name="company" size="40" value="<?php echo $this->escape($this->user->get( 'company' ));?>" class="inputbox required validate-company" maxlength="25" />
</td>
</tr>
<!-- CUSTOM COMPANY INPUT ENDS -->

No comments:

Post a Comment