Custom parameters in com_content - and how to use them

Your code modifications and patches you want to share with others.
tobiasn
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Tue Feb 27, 2007 6:34 pm

Custom parameters in com_content - and how to use them

Postby tobiasn » Wed Aug 27, 2008 8:42 am

I've been fiddling with the idea to add custom fields to my content objects for some time. Sometimes content items should consist of preset fields, header images that needs to adhere to a certain position, ie OVER the header, etc - with minimum need for client f*ckups.

The parameter system of J1.5 came to the rescue, and together with the ingenious override system, it's a match made in heaven.

Only one core hack is needed, and it's very easy to back up and transfer to the next upgrade


Core alterations:

File: administrator\components\com_content\models\article.xml - this is where the parameter fields gets made.

In the top of the advanced parameters block, add the following code:

Code: Select all

   <params group="advanced">
      <param name="title_tag" type="text" default="" label="Custom page title" description="Use this field to enter a custom title-tag for the full view. Leave blank for default" />
      <param name="silly_options" type="list" default="" label="Some options" description="Whatever">
         <option value="Boja">Boja</option>
         <option value="Shaka">Shaka</option>
         <option value="Daka">Daka</option>
      </param>
      <param name="article_image" type="imagelist" default="" label="Article image" description="Choose an image to use in the article header" directory="images/stories/header-img" filter="\.jpg$" />
      <param type="spacer" />
               ...


This will yield the following fields inside the Advanced Parameters-tab when you edit content items:

Image (Norwegian language file)

- A text field parameter named "title_tag"
- A list where you can choose silly parameters, called "silly_options"
- A list that gets the .jpg files from the "images/stories/header-img" folder, called "article_image"
- A nice little spacer to make your custom fields more visible

Check out http://docs.joomla.org/Tutorial:Template_parameters#Standard_parameter_types_in_detail for a nice referral on the different parameter options available to us. Template params use the same system as content params.



Application examples:

Using beez as an example, open \templates\beez\html\com_content\article\default.php

Code: Select all

...
<div id="page">
<?php if ( $this->params->get( 'article_image' ) ) { ?>
<div class="article_img"><img src="images/stories/header-img/<?php echo $this->params->get( 'article_image' ); ?>" alt="article image" /></div>
<?php } ?>
...


This will check if the "article_image" property is set. If so, it adds a div with the image inside. This is really simple - in production I'd probably use some GD stuff to resize/crop the image. It's PHP, so your imagination is the limit.

The "silly_options" we made, could well be used to set a custom article template, be used as a article-class-suffix, etc. I'm sure you all see the possibilities.


Another simple core hack to use the "title_tag" parameter:

In this example, we cannot use the overrides - but it's a simple and portable alteration:

In \components\com_content\views\article\view.html.php, line 99, find:
$document->setTitle( $params->get( 'page_title' ) );

Replace it with:

Code: Select all

      if ( $params->get( 'title_tag' ) ) {
         $document->setTitle( $params->get( 'title_tag' ) );
      } else {
         $document->setTitle( $params->get( 'page_title' ) );
      }


It checks whether you've set the "title_tag" param, if so, uses it - and defaults to the ready-made one if not. Very simple, and a major step up from the 1.0 way.


I'm really eager to get some feedback on this. I feel it's a little step towards a more dynamic content class, which has been a wet dream of mine for some time. Ideally, I'd like to put my own params in their own tab, in their own db field, etc - but my coding skills are at best suited for reverse engineering. If somebody wants to pick it up and develop this further, I'll be very happy!

dharmainternet
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 07, 2008 2:11 am

Re: Custom parameters in com_content - and how to use them

Postby dharmainternet » Wed Aug 27, 2008 5:17 pm

Hi Tobiasn

This is excellent and I think exactly what I need. I would like to assign a unique image to each article. Is it possible to assign it to a module position? I am a joomla newbie so I apologize in advance if the answer is obvious.

Thanks, Tina

tobiasn
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Tue Feb 27, 2007 6:34 pm

Re: Custom parameters in com_content - and how to use them

Postby tobiasn » Thu Aug 28, 2008 6:15 pm

Well, since it's effectively a part of the content item itself, it's not easy to assign it to a module position - it would be much easier to just place it inside the "default.php" override-file for com_content.

I guess you'e got an idea of overrides, if not - this is a nice introduction: http://docs.joomla.org/Understanding_Output_Overrides

I exemplified the case of the article image in the post above, using the beez template's override. The php and html stuff can be daunting at first, but the code I've used here is simple stuff. You can just copy the code over to your own project, but remember this:
- you need to decide on one folder where you will put the images
- there's no way of actually previewing the images automatically using this method - unless you want to dive into some serious coding
- you need to remember to include the full image path in the override

Good luck :)

dharmainternet
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 07, 2008 2:11 am

Re: Custom parameters in com_content - and how to use them

Postby dharmainternet » Sat Aug 30, 2008 12:49 am

Thanks for responding. I'll give it a try!

brien
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Tue Oct 30, 2007 3:58 pm

Re: Custom parameters in com_content - and how to use them

Postby brien » Sun Oct 19, 2008 7:06 am

This is great stuff, I do have a question though. In the above example, with the created "silly Options" drop menu in the params. How would I go about coding something to show only if I chose a certain option. Say I choose the "Boja" option in my article setup and I want it to display some special text in the header of the article how would I code the default.php to say if "silly option/Boja" is selected, echo this?

tobiasn
Joomla! Apprentice
Joomla! Apprentice
Posts: 27
Joined: Tue Feb 27, 2007 6:34 pm

Re: Custom parameters in com_content - and how to use them

Postby tobiasn » Sun Oct 19, 2008 7:17 pm

Hey Brien, that's fairly easy, you check if the variable name contains the appropriate value .

Try this:

Say you want this applicable in every article's full view and you use the Beez template: you'll do this in \templates\beez\html\com_content\article\default.php

Code: Select all


<!-- Somewhere in the code -->

<?php if ( $this->params->get( 'silly_options' ) == "Boja" ) { ?>

<p> Some paragraph that could be any code that only will display if "Boja" is selected. </p>

<?php } // ending the IF block ?>


brien
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Tue Oct 30, 2007 3:58 pm

Re: Custom parameters in com_content - and how to use them

Postby brien » Fri Oct 24, 2008 7:11 am

Thanks for the post, that is exactly what I was looking for, just didn't know how to write the == part. Thank you!!!

brien
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Tue Oct 30, 2007 3:58 pm

Re: Custom parameters in com_content - and how to use them

Postby brien » Mon Oct 27, 2008 5:37 am

Is there a way to make a separate drop menu in the parameters section to put some personal parameters?

mic
Joomla! Guru
Joomla! Guru
Posts: 692
Joined: Thu Aug 18, 2005 10:51 pm
Location: Austria
Contact:

Re: Custom parameters in com_content - and how to use them

Postby mic » Mon Oct 27, 2008 6:35 am

brien wrote:Is there a way to make a separate drop menu in the parameters section to put some personal parameters?

Yes, everything is there: http://docs.joomla.org/Tutorial:Template_parameters#Standard_parameter_types_in_detail
http://www.joomx.com - custom extensions and development
http://www.joomlasupportdesk.com - support, migration, training and consulting
Member of the German Joomla Translation Team

frank_24
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Nov 21, 2008 3:30 am

Re: Custom parameters in com_content - and how to use them

Postby frank_24 » Fri Nov 21, 2008 5:27 am

Hi,

This is great, I was just looking for a way to do this.

The values are stored in the DB in jos_content.attribs.
The only problem is that the parameters do not have their individual columns.
So it makes it hard to query the database and get a list of all the articles where Grejer = Shaka, for example..

cehjohnson
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Oct 25, 2007 10:00 am

Re: Custom parameters in com_content - and how to use them

Postby cehjohnson » Sun Dec 07, 2008 7:34 pm

tobiasn - that's great and thanks for posting it. I'm doing this at the moment and have managed to create the article parameter quite easily, which appears in the form when creating a new article. It isn't, however, saved. How do I see to it that it's saved in the db?

shimmytango9000
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Dec 07, 2008 2:23 am

Re: Custom parameters in com_content - and how to use them

Postby shimmytango9000 » Thu Dec 11, 2008 1:52 am

hey tobaisn!

great post!

is there any way to call article parameters from a module...say, related items? i'm trying to customize related items so that it will also show the article's image.

thanks!

jackprobe
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Sun Oct 28, 2007 12:50 am

Re: Custom parameters in com_content - and how to use them

Postby jackprobe » Thu Dec 11, 2008 6:25 pm

Hi Tobiasn,

Great tutorial .... Can it also do the following ?

The content belongs NOT ONLY to section & categories. But also to 'Product'. So to add a new content, I need to make a new drop down of 'Product' item, besides dropdown of section & category.

Questions :
- Must I add a new field in content table to hold the 'Product' item ? If yes, which table must I change ?
- Which other files must be edited to catch the value and store it to database ?

Any information will be helpful .... many thanks.
Best Regards,
- Jack -

User avatar
rw1
Joomla! Guru
Joomla! Guru
Posts: 958
Joined: Sun Aug 28, 2005 11:23 am
Location: New York
Contact:

Re: Custom parameters in com_content - and how to use them

Postby rw1 » Sat Dec 20, 2008 11:03 pm

this is awesome! thanks tobiasn!

im trying to modify this link reference in a module:

Code: Select all

<a href="<?php echo $item->link; ?>" class="readon"><?php echo $params->get('readmore'); ?></a>

with something that would pick up a custom field from the article parameters - my question is how would i reference that custom field?

ie what could i replace <?php echo $item->link; ?> with?

thank you!

edit:

my process so far:

- added to article.xml

Code: Select all

<param name="alternative" type="text" size="20" default="" label="Alternative Link" description="An Alternative Link" />

my question:

- how do i reference what is added to this field in a module so that it replaces the following link reference:

Code: Select all

<a href="<?php echo $item->link; ?>" class="readon"><?php echo $params->get('readmore'); ?></a>

User avatar
rw1
Joomla! Guru
Joomla! Guru
Posts: 958
Joined: Sun Aug 28, 2005 11:23 am
Location: New York
Contact:

Re: Custom parameters in com_content - and how to use them

Postby rw1 » Sun Dec 21, 2008 12:05 am

cehjohnson wrote:I'm doing this at the moment and have managed to create the article parameter quite easily, which appears in the form when creating a new article. It isn't, however, saved. How do I see to it that it's saved in the db?

i am also finding it is not saved - does anyone know how to keep the field saved?

thank you!

EDIT: It is being saved now when i add it to the 'advanced parameters', and not just the 'article parameters'.

still dont know how to reference the field in a module - can anyone help?

thanks!

ace100uk
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Sat Jan 03, 2009 2:15 am

Re: Custom parameters in com_content - and how to use them

Postby ace100uk » Sun Jan 04, 2009 8:57 pm

This looks like a useful hack - so, it's basically like adding a custom field to the article which can be displayed in the frontend?

Is there any way to use this hack in the frontend article submission form so that users can enter the custom field data?

There's a lot of people looking to add custom fields to the main joomla article content. I've been experimenting with this solution, but its very buggy:
viewtopic.php?f=428&t=352285

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Sat Jan 17, 2009 11:31 am

rw1 wrote:
cehjohnson wrote:I'm doing this at the moment and have managed to create the article parameter quite easily, which appears in the form when creating a new article. It isn't, however, saved. How do I see to it that it's saved in the db?

i am also finding it is not saved - does anyone know how to keep the field saved?

thank you!

EDIT: It is being saved now when i add it to the 'advanced parameters', and not just the 'article parameters'.

still dont know how to reference the field in a module - can anyone help?

thanks!


@rw1: I did some customization too, the difference is that I created these parameters under basic parameters and they are being saved (not sure if they're being saved to the db, but I'm able to retrieve them by just referencing them).


Referencing custom parameters is fairly easy. Let's say you've created parameter called dummy_text and you just want to echo the parameter, so to do it would be:

Code: Select all

echo $item->dummy_text; //simple and easy, eh?


I hope this helps you guys.


Happy coding!
Caio

cehjohnson
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Oct 25, 2007 10:00 am

Re: Custom parameters in com_content - and how to use them

Postby cehjohnson » Sat Jan 17, 2009 11:48 am

So it's possible to give values to these parameters via the content creation gui is it?

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Sat Jan 17, 2009 5:32 pm

cehjohnson wrote:So it's possible to give values to these parameters via the content creation gui is it?


Do you mean when you're posting a new content (or editing an existing one) on the frontend? If this is it, I'm not sure if you can, but I don't see why you can't.

I didn't went that far creating custom parameters, all I did was custom parameters for the com_content on the backend to customize the layout for the "Category List Layout" (I've added the option to hide the '#' column, modified the date shown from creation to 'start publishing' date and added a time column which shows the 'starting publishing' time.

Now I'm playing with the original banners module to give the user (anyone who has backend access) an option to change the banners main folder (which is hard coded inside the mod_banners.php file) to any folder wanted. Later, I'll extend that and the user (that same one who has backend access. lol :laugh: ) will be able to point separately for each file type (i.e. IMG > media/banners/images, SWF > media/banners/flash)



Happy coding,

Caio

cehjohnson
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Oct 25, 2007 10:00 am

Re: Custom parameters in com_content - and how to use them

Postby cehjohnson » Sat Jan 17, 2009 5:39 pm

Yes, i want the user to be able to enter a custom parameter into exactly the same form they use to create the content

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Sat Jan 17, 2009 5:48 pm

@cehjohnson

I'll play with that later, because I also have the need to create a custom field, but I think this will be a bit harder because it will need to deal with the db.

cehjohnson
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Oct 25, 2007 10:00 am

Re: Custom parameters in com_content - and how to use them

Postby cehjohnson » Sat Jan 17, 2009 6:14 pm

Would be great if you could keep me informed thanks!

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Sat Jan 17, 2009 11:24 pm

@cehjohnson:

I'll post here any progress I make, but I have to apologize, this will have to wait. I'm in the middle of a big (reeeally big!!!) task here at work.

Thx for your appreciation.



Happy coding,

Caio

cehjohnson
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Oct 25, 2007 10:00 am

Re: Custom parameters in com_content - and how to use them

Postby cehjohnson » Sun Jan 18, 2009 1:08 pm

Don't worry ;-) Best of luck :-)

BigSexy
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Mon Sep 01, 2008 6:21 pm

Re: Custom parameters in com_content - and how to use them

Postby BigSexy » Thu Jan 29, 2009 11:09 pm

Damn, it seems I can't get it to work...am I the only one?

QuBic
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Jan 29, 2009 11:49 pm
Location: UK
Contact:

Re: Custom parameters in com_content - and how to use them

Postby QuBic » Thu Jan 29, 2009 11:57 pm

Will buckle down and get this hack sorted. Custom fields have always been double dutch to me, this seems a little easier to work through so thanks :)

xclusive
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Jan 30, 2009 12:56 am
Contact:

Re: Custom parameters in com_content - and how to use them

Postby xclusive » Fri Jan 30, 2009 1:47 am

nice...will give it a try soon..
Please read forum rules regarding signatures: viewtopic.php?t=65

godmode
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Jan 29, 2009 6:28 pm

Re: Custom parameters in com_content - and how to use them

Postby godmode » Fri Jan 30, 2009 5:24 am

why not working for you? its not that easy to achieve but you should get there soon. stick to the guide.

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Fri Jan 30, 2009 11:10 am

BigSexy wrote:Damn, it seems I can't get it to work...am I the only one?


Please describe what's happening...

caiorg
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Jun 19, 2008 11:36 pm

Re: Custom parameters in com_content - and how to use them

Postby caiorg » Fri Jan 30, 2009 11:20 am

Just to clarify things a little bit...


Creating custom parameters is indeed easy if you have some programming logics knowledge. Once you figure out the concept it's very easy to implement, but there are some custom parameters that might be harder to implement because it will require more then just echoing or you probably will break something (i.e. your template).


Happy Joomla!ing

Caio


Return to “Core Hacks and Patches”

Who is online

Users browsing this forum: No registered users and 2 guests