Sunday, 6 February 2011

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



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:

<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:

...
<div id="page">
<?php if ( $this->params->get( 'article_image' ) ) { ?>
<div ><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:

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!

No comments:

Post a Comment