Custom parameters in com_content - and how to use them

Your code modifications and patches you want to share with others.
johngunadi
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Feb 09, 2009 10:13 pm

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

Postby johngunadi » Sat Feb 14, 2009 1:41 pm

Editing/adding articles parameters (attribs) from frontend is one thing.
My problem is how to make options/radio-button/text input for the frontend users.
Letting them to manually type
show_title=1
link_titles=1
show_intro=1
show_section=1

to a textarea is the last desperate thing I'd do.

YellowT
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Tue Feb 10, 2009 8:26 pm

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

Postby YellowT » Sun Feb 15, 2009 11:20 am

Excellent post!

Does anyone have any experience in passing the custom article parameters on to a module? I have a module where I have published an article with custom parameters. Unfortunately I am not able to retrieve the custom article parameters in the module code. I was hoping it might be possible to use something like
<?php $params =& $mainframe->getParams('com_content'); ?>
in the module code to retrieve the custom article parameters, but that does not seem to work. Does anyone have any experience with this?

YellowT
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Tue Feb 10, 2009 8:26 pm

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

Postby YellowT » Sun Feb 15, 2009 1:47 pm

I figured out how to use the custom article parameters in a module context. Posting the solution here in case anyone wonders:

What I am actually trying to do is this: I have setup some parameters for my articles. I have published the articles through the Newsflash module, and want to use the article parameters to modify the article appearance inside the module. (Placing and positioning of an image set in custom article parameters).

To access the article parameters from the Newsflash module code, I add the following lines (to _item.php for the module newsflash_mod):

<?php $paramsdata = $item->attribs;
$paramsdefs = JPATH_COMPONENT.DS.'models'.DS.'article.xml';
$params = new JParameter( $paramsdata, $paramsdefs );?>

I can then retrieve the value of a custom article parameter like this:

<?php echo $params->get( CUSTOM_PARAMETER_NAME);?>

Not sure if this is the best way of doing this, but it solved my problem ;)

davey c
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Thu Aug 07, 2008 7:09 am

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

Postby davey c » Wed Feb 18, 2009 5:49 pm

Genius - all of you! This whole thread has really helped me out of a jam. Does anyone know if I can access these new parameters when creating an article in the front end?

Jseb
Joomla! Intern
Joomla! Intern
Posts: 57
Joined: Thu Jan 15, 2009 8:32 pm

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

Postby Jseb » Tue Apr 21, 2009 2:10 pm

Nice tutorial i most admit but i am having an issues

here what i put in article.xml on the following path Joomla/administrator/components/com_content/models/article.xml

Code: Select all

<param name="article_image" type="imagelist" default="" label="Image de l article" description="Choissisez une image qui represente l article" directory="images/stories/" filter="\.jpg$" />


I do see the parameter into the article manager. But when i seletec it and apply or save it goes back to default setting.

I also try the following code on the frontpage layout (overriding) with this

Code: Select all

echo $this->params->get('article_image');
echo $this->item->get('article_image');


but none of them work. I also went into phpmyadmin and look at the database but their only seem to be these following fields

Code: Select all

show_title=1
link_titles=0
show_intro=1
show_section=
link_section=
show_category=0
link_category=0
show_vote=
show_author=0
show_create_date=0
show_modify_date=0
show_pdf_icon=0
show_print_icon=0
show_email_icon=0
language=
keyref=
readmore=




UPDATE: After more troubleshooting and rereading i found that my issue was simply because i didnt put the param into the group advanced. Great tutorial!!!
Did i miss anything?? Also if i have a parameter text with a default link. I cant modify it has it will come back to the default???

Thanks in advance

halcyon_dev
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Fri May 08, 2009 9:13 pm

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

Postby halcyon_dev » Sat May 09, 2009 11:15 pm

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!


quick question...were you able to find out how to reference the parameter in a component/module? I am having what I believe is a similar problem and hoped that you might be able to pass on a solution.

funkzilla
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Fri May 29, 2009 2:17 pm

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

Postby funkzilla » Fri May 29, 2009 2:22 pm

Great post.

Now, I want to access each article's new custom parameter in Blog view (section, category and frontpage). I want to be able to use that parameter for each post preview.

In my example, I am going to have a choice of colour scheme, which is shown in blog view as well as article view, and a preview image (sized displayed the same for every post). I think I could do with with parameters.

Anyone have any advice on this - either with a com_content hack or plugin.

Cheers

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 » Tue Jun 16, 2009 9:18 pm

Hey Peeps,

Sorry I've been ignoring this thread for so long, but it seems like you've all been making good progress.

Since I sussed this out, it's been a standard "hack" for all our solutions - we mainly use it for article images though, for more advanced stuff there's always Zoo by Yootheme - even the free version is very nice for building custom content classes.

What I like about this stuff, though, is that all you need to "hack" is the XML - it hardly constitutes a hack IMHO. All the rest can be accomplished by editing the template's overrides.

Just to brush up: When you've added the params to the admininstrator/components/com_content/models/article.xml file, you should see your params in the Advanced Parameters-tab instantly. If you don't, you've done a boo.

When you're all set with your params, your overrides, php skills and imagination are the limits. You can probably do all of the things you've asked for in this thread, and more. We've actually extended this with some nifty image resizing suff, but it's outside the scope of this thread, and can probably be done with free 3d party stuff faster than I can paste the code.

Before I give some more examples, I've got to link to these again:
http://docs.joomla.org/Tutorial:Template_parameters
http://docs.joomla.org/Understanding_Output_Overrides

Those two, and php.net, was all I needed to suss out this stuff, I'm no coder. :)

Ok, some more examples. We've gone to town with article images, integrated it with JCE Image Manager (some fairly basic JS modifications), and added these options to the XML:

Code: Select all

   <params group="advanced">
      <param name="art_img1" type="text" default="" label="Artikkelbilde 1" description="Artikkelbilde 1" />
...
      <param name="art_img8" type="text" default="" label="Artikkelbilde 8" description="Artikkelbilde 8" />
      
      <param name="art_imgtxt1" type="text" default="" label="Billedtekst 1"
...
      <param name="art_imgtxt8" type="text" default="" label="Billedtekst 8" description="Billedtekst 8" />

      <param type="spacer" />


Yeah, they all go from 1 through 8. I'd love to make it dynamic. I took out a bit for your viewing pleasure. "Artikkelbilde" and "Billedtekst" is Norwegian for "Article Image" and "Image Caption", respectively.

Now, in the template's html/com_content/article/default.php file, we've got 8 ugly blocks like this:

Code: Select all

<?php if ( $this->params->get( 'art_img1' ) ) { ?>
<div class="article_img"><img src="<?php echo $this->params->get( 'art_img1' ); ?>" alt="<?php echo $this->params->get( 'art_imgtxt1' ); ?>" title="<?php echo $this->params->get( 'art_imgtxt1' ); ?>" /><div class=bildetekst><?php echo $this->params->get( 'art_imgtxt1' ); ?></div></div>
<?php } ?>


I hope you see the point.

What I hope you get out of this is this: Whenever you've set up a parameter, you can check for this parameter whenever com_content parses the items containing the params. Article view is straightforward, but in category, section and frontpage views you can fiddle around with the blog_item.phps and default_item.php respectively.

However, you can edit category and section xmls too, get params in their overrides and create several new item-overrides that you can switch around dynamically. For those of you that think of Joomla! as limited in the templating dept - think again.

The fun part is that all the php you really need to know is the if/else and echo stuff, along with your XML and HTML. Basic template for this:

Code: Select all

<?php if ( $this->params->get( 'THE_XML_PARAM_NAME' ) ) { ?>
Do the HTML dance if the param's set to anything
<?php } ?>

<?php if ( $this->params->get( 'THE_XML_PARAM_NAME' ) == "foo" ) { ?>
Do the HTML dance if the param's set to "foo"
<?php } ?>

<?php echo $this->params->get( 'THE_XML_PARAM_NAME' ); ?> outputs "foo"


Shortly, I'll post a piece about how to do this with mod_custom and the module chromes. That's even more fun. :)

Have a blast :)

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 » Tue Jun 16, 2009 10:36 pm


bongobongo
Joomla! Explorer
Joomla! Explorer
Posts: 349
Joined: Mon Dec 15, 2008 11:10 am

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

Postby bongobongo » Thu Aug 13, 2009 5:32 pm

I'm looking for a way to use the imagelist type field and be able to see the image that is selected.

Is that possible in some way?

If not, then is it possible to use some other custom method to select an image and see the image?

Regards

alistairjackman
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Wed Apr 29, 2009 2:22 pm

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

Postby alistairjackman » Tue Aug 25, 2009 12:36 pm

This thread has been very useful to me so far, thanks to all contributors.

I'm trying to do exactly this:
'Ok, some more examples. We've gone to town with article images, integrated it with JCE Image Manager (some fairly basic JS modifications), and added these options to the XML:'


As mentioned above..

Does anyone have any insights as to how to integrate the JCE Image Manager and the parameters so that for example, instead of choosing an image from an imagelist (limited flexibility) the Image Manager can be used to set the image?

MrBreakfastDot
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Sep 03, 2009 9:32 am

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

Postby MrBreakfastDot » Thu Sep 03, 2009 9:35 am

Thanks for the info. I was having a monster of a time getting my main template index.php to grab the custom parameter.

For anyone else having the same problem... here's the article that helped me out:

http://www.theopensourcerer.com/2009/01 ... and-rings/

And here's a code example (placed within the index page's <head></head> it takes the custom parameter and places it into a variable that is easily used in an echo statement):

// This loads a custom article parameter "Ring Menu". The custom parameter is defined in
// administrator/components/com_content/models/article.xml.

$params = &JComponentHelper::getParams( 'com_content' );
$class = $params->get('ring_menu');

bongobongo
Joomla! Explorer
Joomla! Explorer
Posts: 349
Joined: Mon Dec 15, 2008 11:10 am

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

Postby bongobongo » Tue Sep 08, 2009 8:19 am

Hi there:

Adding a new custom parameter to the Advanced group is working great.
And as others has said before this get stored in the attribs field in jos_content table.

Does anybody here know how to add a parameter to the first params group in the article.xml file?
(
the one that starts with:

Code: Select all

<params addpath="/administrator/components/com_content/elements">
<param name="created_by" type="author" default="" label="Author" description="DETAILAUTHOR" />
.....
....
</params>

)

If I add a parameter there I can see it (the field) when editing an article, but when saving the article then data from this field is not saved.
So adding a parameter there probably need changes to some more core files.

If someone knows how to make this work please post it here.

Robert_ITMan
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Sep 18, 2009 8:26 pm

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

Postby Robert_ITMan » Thu Nov 05, 2009 9:39 pm

Just wanted to thank you all for this post and solution, I'm sure this will help some of you:

The following code in the template root index.php (\templates\ja_purity\index.php) file will get the active menu_image. This code along with the code for getting custom article images is my ideal soultion as I now have a header picture for every page either from the menu or the articles themselves. It's a beautiful thing!

Code: Select all

$menu       =& JSite::getMenu();
$item       =  $menu->getActive();
$params     =& $menu->getParams($item->id);
$menu_image =  $params->get( 'menu_image' );

tobiasn's custom param in com_content along with a few if statements did the trick:

Code: Select all

$article_image = &JComponentHelper::getParams( 'com_content' )->get( 'article_image' );

Here it is with the if statements - I noticed I had to put this code after the </head> so Internet Explorer won't lose the css.

Code: Select all

</head>

<?php
// set this_image to null
      $this_image = NULL;
// following gets the menu image
      $menu         =& JSite::getMenu();
      $item         =  $menu->getActive();
      $params       =& $menu->getParams($item->id);
      $menu_image   =  $params->get( 'menu_image' );
     echo '<!-- $menu_image = '.$menu_image.' -->';
// if there is a menu image use it
      if ( $menu_image != '' && $menu_image != -1) {
       $this_image = JURI::base(true).'/images/stories/'.$menu_image;
      }
// following gets the article image
      $article_image = &JComponentHelper::getParams( 'com_content' )->get( 'article_image' );
     echo '<!-- $article_image = '.$article_image.' -->';
// if there is an article image use it
      if ( $article_image != '' && $article_image != -1) {
       $this_image = JURI::base(true).'/images/stories/'.$article_image;
      }
// if there is no article or menu image use default image (pick an image)
      if (is_null($this_image)) {
       $this_image= JURI::base(true).'/images/stories/head_home.jpg';
      }
?>

P.S. tobiasn you should be upgraded to joomla master - I wish you would help write the joomla documentation - your explaination and real example helped me get it.

SarahDev
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Dec 08, 2009 8:28 pm

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

Postby SarahDev » Wed Dec 16, 2009 1:32 pm

tobiasn wrote:What I hope you get out of this is this: Whenever you've set up a parameter, you can check for this parameter whenever com_content parses the items containing the params. Article view is straightforward, but in category, section and frontpage views you can fiddle around with the blog_item.phps and default_item.php respectively.

I am customizing the template files for the menu item type "Category List Layout" (\templates\my_template\html\com_content\category\default.php and \templates\my_template\html\com_content\category\default_items.php), using the beez files as a starting point.

My problem is I don't manage to access my custom article parameters from the default_items.php file. From the article template I just access them by writing "$this->params->get('my_param')", which works just fine. However, when trying the same approach from the category list I get "Fatal error: Call to a member function get() on a non-object in...".

This is the relevant code from the default.php file:

Code: Select all

<?php $this->items =& $this->getItems();
echo $this->loadTemplate('items'); ?>


And this is the relevant code from the default_items.php:

Code: Select all

<?php foreach ($this->items as $item) : ?>
<?php echo $item->params->get('my_param') ?>
...

I am not a PHP or Joomla Pro, but I have tried to review the code in the underlying component (\components\com_content\views\category\view.html.php). I don't know if there's a bug there so the parameters don't get picked up, or if there is some other syntax I should use in my template file. I am hoping some expert might have an answer for me. And quickly! Please!!!
:)

seankillian
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Jul 20, 2010 3:09 am

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

Postby seankillian » Tue Jul 20, 2010 3:29 am

I am having the same issue as SarahDev...
That is, I cannot get my custom article parameters to appear in the category view; specifically in the template over ride here: templates\my_template\html\com_content\category\default_items.php

tobiasn mentions that we need to fiddle around a bit, but I have not managed to find a solution as yet...

Article view is straightforward, but in category, section and frontpage views you can fiddle around with the blog_item.phps and default_item.php respectively.


Any help greatly appreciated.

SarahDev
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Dec 08, 2009 8:28 pm

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

Postby SarahDev » Tue Jul 20, 2010 7:35 pm

I never managed to get it to work with the category view, so I guess there is some bug not passing on the parameter values or something.

However, my workaround was modifying the section blog view instead, and that worked for me. So however irritating to have to do the exact same thing in that file instead, I used that workaround since I didn't have more time to waste...

Good luck, Sean, hope this helps! Maybe the category blog version would work too, I don't remember if I tried that one since it was so long ago. But you could try if it suits you better, in my case using section was ok, I just included a second level for-loop to list the pages instead of the categories.

seankillian
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue Jul 20, 2010 3:09 am

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

Postby seankillian » Wed Jul 21, 2010 2:03 am

Thanks Sarah.
I managed to get the new parameter values to display in the Blog Category view so I just reworked the template there.
Thanks again.


Return to “Core Hacks and Patches”

Who is online

Users browsing this forum: No registered users and 1 guest