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

Kanye

Hi Andres,
Very helpful article!
KW

August 28, 2011 7:32 pm

Jack

Hi,

Have spent over two days looking for similar solution..
So was happy when I found your article – good work and thanks!

September 28, 2011 3:58 pm

Andres Gallo

I am glad it helped.

October 3, 2011 8:48 am

tanura

very good work

November 19, 2011 6:10 am

Kim Que

Thanks so much dude! Really Helpfull Thumbs UP!

November 20, 2011 12:10 pm

Kim Que

Hey Andres,
im so excited to read this tutorial article 🙂 thumbs up!

But I got a problem to give out the field who is for categoryview “blog_item.php”, actually I thought its called the same code “$this->item->japaneseText” for example.

Maybe you could answer my little problem, would be very nice!
Best Regards,
Kim.

November 22, 2011 4:07 pm

Andres Gallo

Hi Kim, I have not been using Joomla for a while. Please let me know if you found a solution to your problem. And if you did, I would love to invite you to share as I am sure it will be helpful to others. Thank you.

December 7, 2011 8:47 pm

him

Thanks andres..really good article.

But i didn’t get one thing “why in part2, path in step 6 and step 7 is same- ‘components\com_content\models\article.php’ ?”

pls reply..

December 12, 2011 12:44 am

Kim Que

Hey,
sorry just forgot to answer.

In Step 6 wee just edit this file for the article view:
components\com_content\models\article.php

and for the category view (blog_item.php) i just add the same like step 6 to the
components\com_content\models\articles.php

It was just the file with S at the end 🙂
hard working for such an little step -.-

hope i could help 🙂

regards,
Kim

December 22, 2011 6:16 am

Andres Gallo

Hi there,

The reason I mention that is so that you follow the same process you did on the previous model and template for the forementioned template and model. The reason is because Joomla uses that different file for its home page, which is the file “default_item.php”, and that file gets content from a different model. There may be other scenarios where joomla uses other templates and views which I may have missed. Should that be the case, the process will most likely be the same to get those pieces to read the new custom fields.

I hope that helped. Feel free to shoot me any questions. I apologize for taking long to reply. I love to be busy, but it makes time management a bit tougher hehe.

December 22, 2011 6:30 pm

Andres Gallo

Hi Kim,

Thanks for replying. I am sure many readers will find your comment very helpful. Thanks

December 22, 2011 6:32 pm

Morphee

Hi Andres,

Thanks for this useful article.
However, you did not explain how to get the new field we had in the form when we edit the article…..
Thanks.

January 6, 2012 11:14 am

Andres Gallo

Hi Morphee. In part one of the article, I am including how to add a new field for the admin part of the interface which includes edit article. Please let me know if this is not what you meant.

January 18, 2012 8:52 pm

proyecto

great article. Im trying to make it works in Joomla 2.5. The part 1 of the tutorial is ok. Im able put the data in the database and edit it later. But im not able to show the value in the front-end. Some files are a little bit diferent in jooma 2.5.

February 23, 2012 5:37 am

Andres Gallo

Wow, They are making updates much faster now. Had no idea they were in 2.5. If anybody posts any suggestions I will update the article. If you figure it out, please post back, I can write edits or a new article for version 2.5.
It should be helpful to a lot of users.
Thanks for reading

February 24, 2012 11:03 am

proyecto

I’ve done it, finaly, just edited some template files like the blog-list (I don’t remember the exact names right now) and others where the articles are listed. And it works great. Thanks again.

March 8, 2012 2:41 am

jojo

Hi Andres!

Thanks for this great tutorial, what I was looking for.

Just one question besides: Do you perhaps know how I can access my custom field from within a module? Maybe calling JRequest or something similar?

Thank again!

April 17, 2012 10:28 am

srinivasanm

What happens when we upgrade the version from 1.7 to 2.5. I mean it, i hope this articles depicts that we were working with the core files, so what will happen when we upgrade the version. Please advice.

April 18, 2012 1:13 am

Andres Gallo

That may work, though I am not sure. Its been a few months since I use Joomla. Been using cmsms, and wordpress for most stuff lately

April 29, 2012 5:36 pm

Andres Gallo

thats why, I showed how to make copies of the modules in the form of a custom module. This is precisely for this reason. So you don’t lose your changes/overrides.

April 29, 2012 5:39 pm

Edvardas

Hello,

Nice tutorial. Helps a lot.
But maybe you know, how to hide custom fields in article?
because now on all articles custom field is displayed. How to add for example: show: yes/no?

Thank you for any suggestion.

July 13, 2012 9:27 pm

Andres Gallo

I am not understanding the question fully. On the template override on step 5, you can add an if statement if there is a condition you want to use for showing and hiding these fields from the front end. If its from the back end admin screen you want to hide them then add a condition on the edit.php (described in part one of tutorial) file. Do a print_r on the joomla variables to see what data you have available for use in the condition. Hope this helps.

July 21, 2012 9:19 am

Uds

Andres,

Seriously spent 2 days looking for this solution. Thanks. Hey I got a question for you since you mentioned cmsms and wp. Do you think CMSMS is a good cms over WP or Joomla? Im planing to stick to one cms rather than playing with all of them. What are you recommondations? thanks.

July 26, 2012 6:49 pm

Andres Gallo

Well I would say it depends on what you are trying to do. Like for blogging wordpress is without doubt the best, though for most medium to large websites that may need lots of admin control my favorite is cmsms. With that said I would say go with wordpress unless you intend to do substantially larger websites where something like joomla or cmsms is more suited. Should that be the case also take a look at Drupal.

Just reading my own recommendation I think I may have made the choice even more confusing. If I was to break down what I like and dislike for each…

Ease of development
The edge over here goes to wordpress. Its extremely intuitive, and it has lots of plugins to do just about everything. I have never used it for larger projects however, because I think it could become cumbersome for more complex projects. CMSMS is in my opinion the best all around cms for developing UI features, that can be easily managed from the cms. Joomla I think is extremely versatile, but I find the way it works very unintuitive. Please keep in mind I have not used Joomla past version 1.7 however.

Ease of maintenance (for non dev user)
CMSMS takes the cake here by a small margin, and I haven’t used their latest version. Joomla once again does not feel as intuitive.

Ease of maintenance/Upgrade (dev wise)
Wordpress is amazing here. Not only is it easy to update and maintain, but has the best support. Joomla is very good here as well, and is supposed to be extremely versatile. CMSMS is very easy to maintain also extremely, however its the only cmsms where I have had to do extra work for having decided to update the cms.

Hope this helps.
CMSMS is my favorite, however I will recommend wordpress because its such a dependable tool with great support, and of course because I typed this comment on wordpress hehe.

July 26, 2012 9:12 pm

Uds

Wow. I never though CMS Made Simple is that powerful. I’ve been using CMSMS for the past 3 years and made shopping carts, directories, websites, gallaries, portfolios and all sort of things with it. But I found WP is more robust and easy to use? I was amazed to hear you choose CMSMS on top of others.

July 26, 2012 9:22 pm

Andres Gallo

Well, CMSMS is very very powerful, however , I recommended WordPress because of the reason you mentioned. Its the most robust Content Management System there is. I could not have thought of a better way to put it.

July 26, 2012 10:14 pm

Uds

Andres, True. Thanks for the tips.

Hey I got a quick question. Can you please tell me what I have to do if I want to output the japaneseText content in the HTML “Title” tag in the template? Actually what I’m trying to do is, have it’s own Title for each article. Hope I make sense

July 26, 2012 10:50 pm

Andres Gallo

Near the end of this article you’ll find where I added a code snippet of an “if” statement which reads as follows. (Im referring to the snipped right before step 6)

1
2
3
4
5
if(preg_match('/japan/', strtolower($lang->getName()))){
    echo $this->item->japaneseText;
   } else {
    echo $this->item->text;
   }

If I remember correctly in that same file (view), you can also find the line where the title is being output, as well as where every other field is being output.
Also look out for step 7 which mentions another view file Joomla uses for its default content article. You can update your views to the exact template/output you want. Just setup some logic to echo what you need based on the specific language you are on.

July 27, 2012 7:38 am

Andres Gallo

I updated my previous reply. Had a bunch of unnecessary info there. Hope that helps.
Do take a look for/into some plugins that may do this functionality by the way. Joomla grows so fast I would not be surprised if there is a plugin for this functionality.

July 27, 2012 4:34 pm

Andres Gallo

Out of curiosity why did you choose CMSMS as well for the past three years.

July 27, 2012 4:36 pm

Uds

Thanks. There are heaps of plugins, but non of them work because my site is custom built and the developers have used some “Categories” as “articles” to maintain the menu structure. Its hard to explain. I’ll give your tut a crack and see. Thanks.

Btw, the reason I used CMSMS because the previous web development firm I worked used CMSMS as their CMS. I honestly have done more than 1,000 CMSMS sites. Most of them are brochure sites but some are pretty damn good ones.

July 27, 2012 6:19 pm

edvardas

hello Andres Gallo,

I added:
name=”myname”
type=”list” label=”JGLOBAL_SHOW_TITLE_LABEL”
description=”JGLOBAL_SHOW_TITLE_DESC”>
JGLOBAL_USE_GLOBAL
JHIDE
JSHOW

in xml file and on administrator panel I see it as well.
but problem is, how to save choice in DB?
if choice “yes”, then custom field should show, if choice is
“no”, custom article should not be shown.
Please advice, thank you in advance.

July 28, 2012 7:08 am

afroz

hi,
thanks a ton for ur post it helped me alot

July 30, 2012 3:48 am

Andres Gallo

If I understand correctly then the issue is saving the value of your new custom field right?

If that is so, you may have missed the step in part one where using sql you add your field to the database. Please keep in mind I did not try all the different field types with the above technique. Dont see why they’d behave different from each other however.

July 30, 2012 10:37 pm

afroz

hi Andres

i have been using ur post and it is quiet usefull.. but its not working for joomla 2.5.. can anyone please help me with this..

July 31, 2012 2:58 am

afroz

item->introtext; echo $this->item->japaneseText;?>

components/com_content/models/article.php

for joomla 2.5

July 31, 2012 4:57 am

afroz

item->introtext; echo $this->item->japaneseText;?>

components/com_content/article/default.php
for joomla 2.5

sory previous comment was a mistake

July 31, 2012 5:00 am

edvardas

afroz
everything is working fine.
check step by step, somewhere is mistake.
which template do you using?
try on default template.

August 1, 2012 2:23 pm

Amir

Hello Andres,
I want to make a secondary category in Joomla. What should I do?
What are the differences with this tutorial?

August 1, 2012 4:50 pm

Andres Gallo

Maybe I’m misunderstanding the question. You can create more categories in the admin panel without modifying Joomla.

August 2, 2012 8:39 am

Amir

No I mean assigning one article to two different categories.
I completed part one of this tutorial successfully. I create a catid2 in the database, and I don’t have any problems in back-end. But I don’t know how to show articles in the front-end. I want to manipulate a module that shows articles in the front-end to fetch articles by their ‘catid2’ and not the default ‘catid’.

August 2, 2012 11:28 am

Andres Gallo

Hi Amir, Im sorry for the delay. You pose a very interesting question? I am not sure how to asnwer your question, without suggesting to further hack the way Joomla brings content together. I haven’t actually tried that myself, but I did find this http://usjoomlapros.com/Extensions/Multi-Categories-For-Articles/flypage.tpl.html. I hope that helps. If you find another way, it would be awesome if you could share it with us.

Thanks

August 6, 2012 8:17 pm

Markus

Hello,

thanks for your great tutorial. One question for Overrides. Not all files have an own custom style, is that right? In files “edit.php” and “article.php” there is no possibility to create an override in my template, is that right?
another question:
how can i integrate an if for my div output like this in the dafault.php

get(‘subtitle’)) : ?>

item->subtitle; ?>

this doesn’t work

August 13, 2012 6:42 am

Markus

Ups. The code will not shwon.
“get(‘subtitle’)) : ?>item->subtitle; ?>”

August 13, 2012 6:44 am

Markus

sorry. i will get an if code around the article.php with a div class. if subtitle in the backend is empty…the div class should not be shown

August 13, 2012 6:47 am

Markus

Hi,

is there a possibility to make overrides for all files we edited? we edit the componentscom_contentmodelsarticle.php. but after an update from joomla, the file will override. One more question: How can i integrate an “if-cause”in default.php for my new field. i use an div around the output field and so…and even when my field is not empty, output my field with the div. is the field in backend is empty…the do not output this code with div style around.

August 14, 2012 11:29 am

Andres Gallo

Hi Markus. The following should work to print the correct layout depending on whether or not your field has returned some content.

1
2
3
if(!empty($this->item->japaneseText) ){//If there is content returned here
    echo '<div class="thisIsMyFieldContent">'.$this->item->japaneseText.'</div>';
}

As for making overrides for every single file the same should be possible for model folder. I haven’t used joomla in quite a bit to know for sure. Please let us know if you find the answer for this. Thanks

August 19, 2012 7:08 pm

Tobi

Hey Andres,
I love this article!!
It works like a charm (in J2.5)

What i need to do further is to show the new fields i made only in BE-edit-forms of all categories under a subcategory:
cat1 -> cat11 Schow fields in edit form
cat1 -> cat12 show
cat1 -> cat13 show

cat2 -> cat21 don’t show

now its shown in all categories

Do you have an idea how to do this ?

Thanks

August 29, 2012 3:39 pm

Andres Gallo

Hi Tobi,

This may not be the cleanest way to go about this, however, you can create some kind of hook in the template view for the admin panel.

What I would create is an associative array associating each field with a category, or set of categories. You can then use this array to present your field or not show them when such is not necessary. I am not a big fan of hooks to be honest, but it may be the simplest way to achieve this without hacking into the core, or going about creating a new plugin. Let me know if this helps at all. Thanks.

Please share what you come up with if you can 🙂
Could help others facing the same issue.

Thanks

August 30, 2012 8:41 pm


Comment now!