Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: [HOW TO] ADD & WORK WITH PARAMS

[HOW TO] ADD & WORK WITH PARAMS 14 Jan 2013 09:28 #6441

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Fellow cooks,

Adding component & object parameters to your component is quite straightforward as both Joomla and Cook have added most of the capability for you. Specifically, Joomla implements JRegistry and COOK has already added a params column to every table in your component.

So, how can you leverage this good stuff in your application?

NOTE:
  • Any component wide 'params' you add to your configuration in the administrator side of your component (config.xml) will be handled automatically.
  • Regarding table-specific parameters, JForm style fields/fieldsets can be added to the relevant admin/models/forms/[model_name].xml. Unlike the main config (above), in order to store/retrieve table level params, you need to overload the bind & load methods in your table class.
TODO:
  1. Go to your table class, admin/tables/[your_table].php and add the following;
    public function bind($array, $ignore = '') 
    /**
             * Overloaded bind function
             *
             * @param       array           named array
             * @return      null|string     null is operation was satisfactory, otherwise returns an error
             * @see JTable:bind
             * @since 1.5
             */
    {
            if (isset($array['params']) && is_array($array['params'])) 
            {
                    // Convert the params field to a string.
                    $parameter = new JRegistry;
                    $parameter->loadArray($array['params']);
                    $array['params'] = (string)$parameter;
            }
            return parent::bind($array, $ignore);
    }
    
    /**
     * Overloaded load function
     *
     * @param       int $pk primary key
     * @param       boolean $reset reset data
     * @return      boolean
     * @see JTable:load
     */
    public function load($pk = null, $reset = true) 
    {
            if (parent::load($pk, $reset)) 
            {
                    // Convert the params field to a registry.
                    $params = new JRegistry;                 
                   // loadJSON is @deprecated    12.1  Use loadString passing JSON as the format instead.
                   // $params->loadString($this->item->params, 'JSON');
                   // "item" should not be present.
                   $params->loadJSON($this->params);
    
                    $this->params = $params;
                    return true;
            }
            else
            {
                    return false;
            }
    }
  2. To display the parameter options on the admin side, update admin/views/[your_component/tmpl/[your_form].php
    • Inside the <form> tags and after your main fieldset(s), add:
      $params = $this->form->getFieldsets('params'); near the top of your file
    • Now, output $params in a foreach loop, rendering them however you want. N.B. If you want to add them in standard Joomla format, use the <div class="width-40 fltrt">. For more info and code, see the Joomla Docs for more info
  3. Now, we must update the frontend to respond to the params, global and model specific. So,
    • go to your site/models/[item_model_name].php and in your populateState(), add:
      // Load the parameters.
      $params = $app->getParams();
      $this->setState('params', $params);
      parent::populateState();
    • Then, in the getItem(), add:
      // Load the JSON string
      $params = new JRegistry;
      // loadJSON is @deprecated    12.1  Use loadString passing JSON as the format instead.
      //$params->loadString($this->item->params, 'JSON');
      $params->loadJSON($this->item->params);
      $this->item->params = $params;
       
      // Merge global params with item params
      $params = clone $this->getState('params');
      $params->merge($this->item->params);
      $this->item->params = $params;
  4. Now, in your view, you can evaluate the parameters and conditionally display certain elements according to your requirements. To do so, got to site/views/[your_view]/tmpl/default.php and target the value of your parameters using
    //my_param is the name of your parameter
    $this->item->params->get('my_param');
Now your component can handle storing, retrieving and acting upon your component's configuration. This can easily be extended now to add menuitem parameter overrides.

Hope it helps!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 30 Jan 2013 12:25 by JoomGuy. Reason: FIXED: broken link
The administrator has disabled public write access.
The following user(s) said Thank You: admin, Tomaselli, edwardcox, dyoungers, koenvdz

Re: [HOW TO] ADD & WORK WITH PARAMS 26 Jan 2013 08:17 #6519

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
BTW, adding params to menu items is done in the metadata.xml file for your layout.

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 28 Jan 2013 20:48 #6546

  • arried
  • arried's Avatar
  • Offline
  • New Member
  • Posts: 1
  • Karma: 0
First of all, It's a great component.
Second, since I am a nubie, I am not clearly understand with your tutorial.
Question:
  1. Where can I modify those files? after installing the component, or before installing component (modify the package)
  2. If modified package : in point 3, go to your site/models/[item_model_name].php I don't see any getItem() function, so where can I put this script?
  3. how do I add a query limit parameter, so I can create results differently, as much as i want

Thanks
Last Edit: 28 Jan 2013 20:55 by arried. Reason: add question
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 30 Jan 2013 12:47 #6556

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @arried and welcome to the Cook forum!arried wrote:
First of all, It's a great component...
@admin has indeed created a very beautiful baby ;)arried wrote:
Question:
  1. Where can I modify those files? after installing the component, or before installing component (modify the package)
  2. If modified package : in point 3, go to your site/models/[item_model_name].php I don't see any getItem() function, so where can I put this script?
  3. how do I add a query limit parameter, so I can create results differently, as much as i want
In answer to your questions;
  1. You'll need to make these changes in your downloaded component.
  2. If you do not see a getItem() method in your model, just add it and be sure to call the parent::getItem(). This is inherited remember...
  3. Presumably, you're referring to a LIMIT clause in a query right? If so, just check out the prepareQuery() method in your model. Also, if you need any further help on this, have a quick search around the forum and post in the appropriate topic/board if necessary as I believe the answer to this exists... ;)
Hope it helps,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 11:25 #6702

  • soportedo
  • soportedo's Avatar
  • Offline
  • Junior Member
  • Posts: 20
  • Thank you received: 1
  • Karma: 1
Hi audibleid

I have a problem with params... I did all that you described, it save the params ok in the DB but it doesn't show the correct value when I load the page, example: if I set in two params to true it shows false when it loads (the admin backend), but the values are OK in the dabase table.

What went wrong?

Thanks in advance.
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 11:35 #6703

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hmmm,

Strange! Are you evaluating their values in IF/ELSE or ternary or just echoing them to the screen regardless?

Also, are you assigning a default value in the get() as a safety? If you're not, try setting the default to something other than true or false to heck that it's reading the value OK - if you get your non boolean value back, you acn be sure that it is not retrieving it correctly.

Then, check if you mis-typed your param name or something...

If you are evaluating them:
Well, it depends on how you are checking them, I mean, are they proper boolean values like 0/1 OR true/false (TRUE/FALSE) or are they strings?

The reason I ask is, if you're checking for a non existing var (param) like if (!$myParam){...} then if it un set or set false then you effectively have no parameter. That is why it is almost always advisable to set a default value for your params.

Anyway,

Hope it helps!

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 11:52 #6704

  • soportedo
  • soportedo's Avatar
  • Offline
  • Junior Member
  • Posts: 20
  • Thank you received: 1
  • Karma: 1
hi, i'm not evaluating nothing... this is the code

this is the XML
<?xml version="1.0" encoding="utf-8"?>
<form>
	<fieldset name="oficio.form" addfieldpath="/administrator/components/com_doportalhogar/models/fields">
		<field name="oficio"
				label="DOPORTALHOGAR_FIELD_OFICIO"
				alias="oficio"
				filter="STRING"
				type="cktext"/>
		<field name="icono" type="media"
			label="DOPORTALHOGAR_FIELD_ICONO"
			size="20"
			directory="oficios"
			class="inputbox"
			default=""
		/>
	</fieldset>
	<fields name="params">
		<fieldset name="params" label="Parametros">
			
			<field
				name="campos"
				type="checkboxes"
				label="campos"
				description="campos" >
					<option value="id_valor_por_presupuesto">Valor por presupuesto</option>
					<option value="id_garantia">Garantia</option>
			</field>
		</fieldset>
	</fields>
</form>

this is the view
<?php
$params = $this->form->getFieldsets("params");
?>

<div class="width-40 fltrt">
<?php echo JHtml :: _ ( 'sliders.start' , 'helloworld-slider' ) ;
	foreach ( $params as $name => $fieldset ) :
		echo JHtml :: _ ( 'sliders.panel' , JText :: _ ( $fieldset -> label ) , $name . '-params' ) ;
		if ( isset ( $fieldset -> description ) && trim ( $fieldset -> description ) ) : ?>
			<p class="tip"> <?php echo $this -> escape ( JText :: _ ( $fieldset -> description ) ) ; ?> </p>
		<?php endif ; ?>
		<fieldset class="panelform" >
		<ul class="adminformlist">
        
			<?php foreach ( $this -> form -> getFieldset ( $name ) as $field ) : ?>
            <?php // echo '*<div align="left"><pre>'; print_r( $field  ); echo "</pre></div>*"; exit; ?>
            
				<li> <?php echo $field -> input ; ?> <?php // echo (int)$field -> getValue ; ?> </li>
                
                
                
			  <?php endforeach ; ?>
		</ul>
		</fieldset>
	<?php endforeach ; ?>
<?php echo JHtml :: _ ( 'sliders.end' ) ; ?>
</div>

do you see anything weird?
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 12:04 #6705

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @sportedo,

No I don't see anything weird...

Since you're adding these params to a view, I can only presume that you're adding these params to a specific table as opposed to component-wide, right?

Do you have the params set in your model's populateState()?
Do you have the JRegistry set up in your model's getItem()?

If all of that is set OK, then, I would set your bool param as a Radio instead of a checkbox to avoid the implicit false state. That way, you're param is explicitly set to false/true and if empty, your default value will override it.

Hope it helps...

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 12:19 #6706

  • soportedo
  • soportedo's Avatar
  • Offline
  • Junior Member
  • Posts: 20
  • Thank you received: 1
  • Karma: 1
Hi, this is the params from a table specific, just to see it in the admin view of the component, should I do the populateState() and getItem() asswell?

this are my functions.
public function populateState($ordering = null, $direction = null)
	{
		$app 		= JFactory::getApplication();
		$session 	= JFactory::getSession();
		
		// Load the parameters.
		$params = $app->getParams();
		$this->setState('params', $params);
		parent::populateState();

		parent::populateState($ordering, $direction);
	}

	public function getItem($pk = null)
	{

		if ($item = parent::getItem($pk)) {
			
			// Load the JSON string
			$params = new JRegistry;
			// loadJSON is @deprecated    12.1  Use loadString passing JSON as the format instead.
			//$params->loadString($this->item->params, 'JSON');
			$params->loadJSON($this->item->params);
			$this->item->params = $params;
			 
			// Merge global params with item params
			$params = clone $this->getState('params');
			$params->merge($this->item->params);
			$this->item->params = $params;			
		}


		return $item;
	}

The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 12:35 #6707

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Yes, you do need the populateState() & getItem() modifications to override a specific table. The only time you don't is in the component's global config.

Have you tried with them added?

Have you tried working with basic, component-wide params first?

If you're still having issues, have you tried var_dump() on your item? do you see your params there?

It's half-term here so taking my daughter to an indoor play area for a few hours. If they have WiFi, I'll check out any replies when I get there.

Hope it helps...

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 12:46 #6708

  • soportedo
  • soportedo's Avatar
  • Offline
  • Junior Member
  • Posts: 20
  • Thank you received: 1
  • Karma: 1
Thanks audibleid,

I did tried everything you said, when I did a vardump it retrieve the params but when in somewhere it sets to null and dissapears when it get to the view. we are reviewing the code and we think in the bindLevel() it looses the params...

thanks for your help!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 12:53 #6709

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
No probs!

Please share any of your findings. I'll do my best to help if you're still stuck!

Have you checked the joomla docs (just in case I missed something) - although I'm sure I haven't. I have it working in a previous project...

Gez


PS Play time ;) check in on this in a short while
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 12 Feb 2013 18:23 #6713

  • soportedo
  • soportedo's Avatar
  • Offline
  • Junior Member
  • Posts: 20
  • Thank you received: 1
  • Karma: 1
hi @audibleid ! we were able to get it work!

there was a problem in jcook jmodel.list.php in the function pupulateparams() , this function sets the params plus the access, so it transforms the params in to a protected object, so in the view was unable to read the values of the defined params.

So.. in the pupulateparams I check if its the view hat I need this params and let set it to an array instead of a protected object....

I thinks there must be a better and correct way to do this... but its a start in order to "fix" this, I hope the admin in a future will add the custom params within j-cook

Thanks for all your help
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 13 Feb 2013 08:34 #6724

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi @soportedo,

Thanks for sharing your findings!

I know he's super busy working on V2.0 but I'll post this to @admin so that when he gets a chance, he can advice us best how to proceed the 'proper' way!

K++

Thanks,

G
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: [HOW TO] ADD & WORK WITH PARAMS 13 Feb 2013 15:17 #6732

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
I am very busy in fact.

I am preparing the workshop for the Joomla Day (France), and 2.0 MUST BE FINISHED.
2.0 is almost finished and really powerfull, cross compat all version (since 1.6), forkable, and much more...
I only need time to do all the tests.
I am actually testing it, and solving the last issues, but this is huge because I have 4 sandboxes to test, and it is a nightmare ;-) (1.6 - 1.7 - 2.5 - 3.0)
All these versions are really differents and 2.0 is providing a kind of layer to do not have to think about cross compatibilities. It does the jod and you only have to think in the 3.0 way of coding (wich is almost the same than 2.5)
Missing classes are handled in the component for previous versions support. (Legacy folder)
(I will copy this to another post discussion about 3.0)

About your question :
'params' are replaced by 'attribs' in 2.0. It will fix your problem.
Coding is now a piece of cake
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.172 seconds

Get Started