360php.net - Web developing and Web Technologies

You are here: Home Tutorials How to add custom field to any Joomla component and display it on the backend, Joomla 1.6, 1.7, 2.5

How to add custom field to any Joomla component and display it on the backend, Joomla 1.6, 1.7, 2.5


E-mail Print PDF

In this tutorial I will show you how to add custom field to any Joomla component (This is applicable for Joomla 1.6 and above).

We will take contacts component as an example and add a Title filed to it (Mr., Dr., etc.)

Let us start:

We will be editing the following files:

    • For the grid display of the backend:
      • administrator/components/com_contact/views/contacts/tmpl/default.php
      • administrator/components/com_contact/models/contacts.php
    • For the Edit view of selected item
      • administrator/components/com_contact/views/contact/tmpl/edit.php
      • administrator/components/com_contact/models/forms/contact.xml

1. Add new filed “title” to the table jos_com_contact_details using PHPmyAdmin

ALTER TABLE 'jos_com_contact_details' ADD title text

2. Open “administrator/components/com_contact/views/contacts/tmpl/default.php”

Between the following code (at “//Add code here”" comment):

<table class="adminlist">
<thead> 
 <tr> 
<th width="1%">
<input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
</th>
// Add code here
<th>
<?php echo JHtml::_('grid.sort', 'COM_CONTACT_FIELD_NAME_LABEL', 'a.name', $listDirn, $listOrder); ?>
</th>

add the following code:

<th> 
<?php echo JHtml::_('grid.sort', 'JGLOBAL_TITLE', 't.title', $listDirn, $listOrder); ?>
</th>
So the final code would look like:

 <table class="adminlist"> 
<thead>
<tr>
<th width="1%">
<input type="checkbox" name="checkall-toggle" value="" title="<?php echo JText::_('JGLOBAL_CHECK_ALL'); ?>" onclick="Joomla.checkAll(this)" />
</th>
<th>
<?php echo JHtml::_('grid.sort', 'JGLOBAL_TITLE', 't.title', $listDirn, $listOrder); ?>
</th>
<th>
<?php echo JHtml::_('grid.sort', 'COM_CONTACT_FIELD_NAME_LABEL', 'a.name', $listDirn, $listOrder); ?>
</th>




Between the following code (at “//Add code here”" comment):

<tr class="row<?php echo $i % 2; ?>"> 
<td class="center">
<?php echo JHtml::_('grid.id', $i, $item->id); ?>
</td>
//Add code here
<td>


add the following code:

<td> 
<?php echo $item->title ?>
</td>



So the final code would look like:

<tr class="row<?php echo $i % 2; ?>"> 
<td class="center">
<?php echo JHtml::_('grid.id', $i, $item->id); ?>
</td>
<td>
<?php echo $item->title ?>
</td>
<td>


3. Open “administrator/components/com_contact/models/contacts.php”




Between the following code (at “//Add code here”" comment):

public function __construct($config = array()) 
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'id', 'a.id',
//Add code here
'name', 'a.name',
'alias', 'a.alias',

add the following code:

'title', 'a.title',

So the final code would look like:

public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'id', 'a.id',
'title', 'a.title',
'name', 'a.name',
'alias', 'a.alias',

Replace the following code:

// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid, a.user_id' .
', a.published, a.access, a.created, a.created_by, a.ordering, a.featured, a.language'.
', a.publish_up, a.publish_down'
)
);


By:

// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.title, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid, a.user_id' .
', a.published, a.access, a.created, a.created_by, a.ordering, a.featured, a.language'.
', a.publish_up, a.publish_down'
)
);


4. Open “administrator/components/com_contact/views/contact/tmpl/edit.php”




Between the following code (at “//Add code here” comment):

<li><?php echo $this->form->getLabel('name'); ?>
<?php echo $this->form->getInput('name'); ?></li>


//Add code here

<li><?php echo $this->form->getLabel('alias'); ?>
<?php echo $this->form->getInput('alias'); ?></li>

add the following code:

<li><?php echo $this->form->getLabel('title'); ?>
<?php echo $this->form->getInput('title'); ?></li>

So the final code would look like:

<li><?php echo $this->form->getLabel('name'); ?>
<?php echo $this->form->getInput('name'); ?></li>
<li><?php echo $this->form->getLabel('title'); ?>
<?php echo $this->form->getInput('title'); ?></li>

<li><?php echo $this->form->getLabel('alias'); ?>
<?php echo $this->form->getInput('alias'); ?></li>


5. Open “administrator/components/com_contact/models/forms/contact.xml”


Between the following code (at “//Add code here” comment):

<field name="id"
type="text"
label="JGLOBAL_FIELD_ID_LABEL"
description="JGLOBAL_FIELD_ID_DESC"
size="10"
default="0"
readonly="true"
class="readonly"
/>


//Add code here

<field name="name"
type="text"
label="COM_CONTACT_FIELD_NAME_LABEL"
description="COM_CONTACT_FIELD_NAME_DESC"
class="inputbox"
size="30"
required="true"
/>


add the following code:

<field name="title"
type="text"
label="JGLOBAL_TITLE"
class="inputbox"
size="20"
/>

So the final code would look like:

<field name="id"
type="text"
label="JGLOBAL_FIELD_ID_LABEL"
description="JGLOBAL_FIELD_ID_DESC"
size="10"
default="0"
readonly="true"
class="readonly"
/>

<field name="title"
type="text"
label="JGLOBAL_TITLE"
class="inputbox"
size="20"
/>
<field name="name"
type="text"
label="COM_CONTACT_FIELD_NAME_LABEL"
description="COM_CONTACT_FIELD_NAME_DESC"
class="inputbox"
size="30"
required="true"
/>

 

That is it. Now you can see the results as in the following screen shots:

Gird view

 

image

Edit view

image

Last Updated ( Saturday, 18 August 2012 14:50 )  

Latest Articles

Comments  

 
0 #11 change 2014-07-04 14:04
Hello there! This is my first comment here so
I just wanted to give a quick shout out and tell you I truly enjoy reading through
your blog posts. Can you suggest any other blogs/websites/forums that
cover the same topics? Appreciate it!
Quote
 
 
0 #10 rowby 2013-12-13 16:45
HI Juan

Can you be more specific regarding "Since joomla 1.6 custom fields can be added through plugins without hack core files...." Perhaps providing a link on the procedure?

Thanks

Rowby
Quote
 
 
0 #9 jursto 2013-09-08 11:06
Hi Mostafa, and thanks for your contribution to the online Joomla community. I'm faced with a simple yet daunting task. Change a fieldname on a form in an Joomla classifieds Ads component. Where would be the best place to look at?
Quote
 
 
0 #8 Natacha 2013-08-09 09:24
Exactly what I what looking for, I wanted to add a "company" field in com_user and display it in administration user list. Thanx a lot ( this tut works in joomla 3.1 )
Quote
 
 
0 #7 lulu 2013-07-01 08:02
hi, i already found the solution regarding the label. thanks. but right now i need to figure out how to display the fields on the front end. do you have any tutorial about that?
Quote
 
 
0 #6 lulu 2013-07-01 04:51
thanks for the tutorial. i've tried this and succeed. but instead to add field 'title', i'd like to add field 'nip'. i already make the change but at the back end it still show 'title'. where did i do wrong? i'm not familiar with php. what's the meaning of JGLOBAL_TITLE? do i have to change it too? what if the 'nip' field is contain numbers only? what's the difference between a.title and t.title? thanks in advance.
Quote
 
 
0 #5 KKJ 2013-06-20 14:45
Hi, Thanks for this great article. Could you please guide how to style a grid sorting heading?

Like this is the original code:
Date

I need to change class="somethingelse"

Thanks
Quote
 
 
0 #4 Wan 2013-04-11 04:43
Nice explanation there. Useful for developer who want to write their own extension
Quote
 
 
0 #3 Gurvinder Garg 2013-04-02 21:54
I have read your site. In this site there are various tutorials related to languages. It is very important for php training students and also important for php developers.
Quote
 
 
0 #2 sam 2013-03-20 12:18
i am completely new to joomla. i have created a component in joomla1.7.1 using component creator . i want to create a table displaying the users from the users table in that component.
any help would be greatly appreciated.
Quote
 

Add comment


Security code
Refresh

About me

Moemen Mostafa is the founder and the wirter of 360php.net...
Moemen Mostafa's profile photo

Related Articles