The Joomla! Forum ™





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 48 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Wed Aug 27, 2008 8:42 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Feb 27, 2007 6:34 pm
Posts: 27
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 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:
      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!


Top
 Profile  
 
PostPosted: Wed Aug 27, 2008 5:17 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Thu Aug 07, 2008 2:11 am
Posts: 4
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


Top
 Profile  
 
PostPosted: Thu Aug 28, 2008 6:15 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Feb 27, 2007 6:34 pm
Posts: 27
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 :)


Top
 Profile  
 
PostPosted: Sat Aug 30, 2008 12:49 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Thu Aug 07, 2008 2:11 am
Posts: 4
Thanks for responding. I'll give it a try!


Top
 Profile  
 
PostPosted: Sun Oct 19, 2008 7:06 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Oct 30, 2007 3:58 pm
Posts: 12
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?


Top
 Profile  
 
PostPosted: Sun Oct 19, 2008 7:17 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Feb 27, 2007 6:34 pm
Posts: 27
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:

<!-- 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 ?>



Top
 Profile  
 
PostPosted: Fri Oct 24, 2008 7:11 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Oct 30, 2007 3:58 pm
Posts: 12
Thanks for the post, that is exactly what I was looking for, just didn't know how to write the == part. Thank you!!!


Top
 Profile  
 
PostPosted: Mon Oct 27, 2008 5:37 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Oct 30, 2007 3:58 pm
Posts: 12
Is there a way to make a separate drop menu in the parameters section to put some personal parameters?


Top
 Profile  
 
PostPosted: Mon Oct 27, 2008 6:35 am 
Joomla! Guru
Joomla! Guru

Joined: Thu Aug 18, 2005 10:51 pm
Posts: 697
Location: Austria
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


Top
 Profile  
 
PostPosted: Fri Nov 21, 2008 5:27 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Nov 21, 2008 3:30 am
Posts: 9
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..


Top
 Profile  
 
PostPosted: Sun Dec 07, 2008 7:34 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 25, 2007 10:00 am
Posts: 10
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?


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 1:52 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Dec 07, 2008 2:23 am
Posts: 2
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!


Top
 Profile  
 
PostPosted: Thu Dec 11, 2008 6:25 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sun Oct 28, 2007 12:50 am
Posts: 20
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 -


Top
 Profile  
 
PostPosted: Sat Dec 20, 2008 11:03 pm 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Sun Aug 28, 2005 11:23 am
Posts: 958
Location: New York
this is awesome! thanks tobiasn!

im trying to modify this link reference in a module:

Code:
<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:
<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:
<a href="<?php echo $item->link; ?>" class="readon"><?php echo $params->get('readmore'); ?></a>


Top
 Profile  
 
PostPosted: Sun Dec 21, 2008 12:05 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Sun Aug 28, 2005 11:23 am
Posts: 958
Location: New York
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!


Top
 Profile  
 
PostPosted: Sun Jan 04, 2009 8:57 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Sat Jan 03, 2009 2:15 am
Posts: 20
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


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 11:31 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
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:
echo $item->dummy_text; //simple and easy, eh?


I hope this helps you guys.


Happy coding!
Caio


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 11:48 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 25, 2007 10:00 am
Posts: 10
So it's possible to give values to these parameters via the content creation gui is it?


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 5:32 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
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


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 5:39 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 25, 2007 10:00 am
Posts: 10
Yes, i want the user to be able to enter a custom parameter into exactly the same form they use to create the content


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 5:48 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
@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.


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 6:14 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 25, 2007 10:00 am
Posts: 10
Would be great if you could keep me informed thanks!


Top
 Profile  
 
PostPosted: Sat Jan 17, 2009 11:24 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
@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


Top
 Profile  
 
PostPosted: Sun Jan 18, 2009 1:08 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Oct 25, 2007 10:00 am
Posts: 10
Don't worry ;-) Best of luck :-)


Top
 Profile  
 
PostPosted: Thu Jan 29, 2009 11:09 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Sep 01, 2008 6:21 pm
Posts: 15
Damn, it seems I can't get it to work...am I the only one?

_________________
http://www.emule-gratis.it | http://abbonamentoaltroconsumo.[URL banned].com


Top
 Profile  
 
PostPosted: Thu Jan 29, 2009 11:57 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Thu Jan 29, 2009 11:49 pm
Posts: 2
Location: UK
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 :)


Top
 Profile  
 
PostPosted: Fri Jan 30, 2009 1:47 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Jan 30, 2009 12:56 am
Posts: 8
nice...will give it a try soon..

_________________
Please read forum rules regarding signatures: viewtopic.php?t=65


Top
 Profile  
 
PostPosted: Fri Jan 30, 2009 5:24 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jan 29, 2009 6:28 pm
Posts: 10
why not working for you? its not that easy to achieve but you should get there soon. stick to the guide.


Top
 Profile  
 
PostPosted: Fri Jan 30, 2009 11:10 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
BigSexy wrote:
Damn, it seems I can't get it to work...am I the only one?


Please describe what's happening...


Top
 Profile  
 
PostPosted: Fri Jan 30, 2009 11:20 am 
Joomla! Apprentice
Joomla! Apprentice

Joined: Thu Jun 19, 2008 11:36 pm
Posts: 12
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 48 posts ]  Go to page 1, 2  Next



Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group