As promised, here is part 2, to the article “add custom content fields to Joomla.” Last monday we discussed the first aspect of adding custom content fields to Joomla which involved all of admin side steps. These steps included adding a field(s) to the administration’s add/ edit article pages, and connecting the new field(s) so that they write to our joomla database. In part two of the tutorial, I am now going to expand on what was covered last time so that we can actually output the information from our new fields.

For those of you who missed part one, you can find the link here.

Please don’t freak out, sections 1 through 4 are on part 1 hehe

Retrieving data from new fields for output

5. Begin by modifying the content component’s view. As this time will be modifying it on the front end, you will need to go to components\com_content\views\article\default.php

An override can be done instead of altering the original file. This is done in your site template where this override is to take place. You can follow the steps for overriding in part one of the tutorial. The only things that will be different are the paths. This time we are creating an override for components\com_content\views\article\default.php instead

Near the end in the default.php file, we find the lines which output the data entered in the articles’ content field.
1
2
3
<?php if ($params->get('access-view')):?>
   <?php echo $this->item->text; ?>
   <!-- item->text is the default textbox which will duplicate with our field name-->
Lets add our new field right before the default field.
1
2
   <?php echo $this->item->japaneseText; ?> <!-- notice I used japaneseText which I have been using for my textbox -->
   <?php echo $this->item->text; ?>
If you have other languages installed you can even display one or the other with using a conditional, which is easier to maintain in many circumstances than the built in method of managing languages.

1
2
3
4
5
   if(preg_match('/japan/', strtolower($lang->getName()))){
    echo $this->item->japaneseText;
   } else {
    echo $this->item->text;
   }
6. Allow Joomla’s front end to read our new field from the database. For this we edit the model in components\com_content\models\article.php
In this file there is a select statement that hooks up the fields for output. Add your new field with prefix ’a.’ In my case for example I add ‘a.japaneseText.’
1
2
3
   $query->select($this->getState(
    'item.select', 'a.id, a.asset_id, a.title, a.alias, a.title_alias, a.introtext, a.fulltext, a.japaneseText,'
  //notice at the end I added 'a.japaneseText'
7. Do the same to the template at components\com_content\views\featured\tmpl\default_item.php, as well as to the model at components\com_content\models\article.php
In the template echo the new field, and on the model add the field to the select statement just as we did on steps 5 and 6.

At this point you are ready to add content to your new fields.

In the xml file we edited on part one of the tutorial, there are other types of fields you can use. Check out some of the other available types of fields to see what else can be added

you can also change the type of input field you are adding. Take a look at other fields in that xml file to see some of the possibilities.

  • Just as the component was overridden modules can be overridden as well.

    • For example in the front end, we can override how the latest articles list outputs, by using the same name as in modules\mod_articles_latest\tmpl\default.php.

      • We copy this once again into our template in the html folder
      • Templates\ourFrontEndTemplate\html\mod_articles_latest\default.php in this case would be our override. We do not need any of the other files in the original module folder.
      • In the case of the mod_articles_latest module, you can modify the output to be more dynamic, or to count the number of items you have and more.
  • Doing the overrides you may notice some index.html files which seem to have no purpose. It’s a good idea to copy these in every directory you create, as these are there for security reasons.

I look forward to hearing your feedback and suggestions on this topic as well as any tricks on web design.

Share this:

Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter
If you enjoyed this post, make sure you subscribe to my RSS feed!

Comments RSS and TrackBack Identifier URI ?
Do you want to comment?

59 responses

Mika

Hi
Nice job my friend.
but
The first part works perfect but secound part doesn’t work on j 2.5!
i don’t know what to do?

September 21, 2012 11:48 am

edvardas

Mika,
both parts are working fine on Joomla 2.5.
Where you struggle? Provide more information.
I suggest you to follow all steps and do it as it is and will be fine.

September 30, 2012 3:42 am

rokon

This article help me to doing text-box

December 5, 2012 9:47 pm

Simon

I am using Joomla 2.5.8 and everything works correct until step . I can no show the new field – a subtitle – in the frontend.
As I read step 7, the second path – components/com_content/models/article.php – is the same as in step 6. Or am I missing something.

Also in the first path in step 7 – components/com_content/views/featured/tmpl/default_item.php – I can not figure out what or where to place what.

December 26, 2012 3:42 pm

Mint

Everything works fine..
But it conflicts to the JS of the plugin that i am using..

July 25, 2013 5:21 pm

Sadaf

I want to add price field after the title field in article.. I done it from back-end successfully but not know how to show it on front-end. I also read the 2nd part of this tutorial but not success.. Anyone help me please.. Thanks is advance..
Regards: Sadaf Ajmal

August 31, 2014 11:49 pm

Amin

hi,
i not found this step5
“”
5. Begin by modifying the content component’s view. As this time will be modifying it on the front end, you will need to go to componentscom_contentviewsarticledefault.php””

default.php in ==>componets-com_content-views-article-tmpl

ok?

March 19, 2015 4:56 am

Andres Gallo

Joomla has changed quite a bit since I wrote this blog post.

April 5, 2015 12:45 pm

Mohamad Ali

The only & the best tutorial about add custom field to content
tnx a lot

April 28, 2015 3:13 am

Comment now!
















Trackbacks