Variables for view - how can I get them?

647 views
Skip to first unread message

Grzesiek

unread,
May 11, 2012, 2:45:55 AM5/11/12
to Joomla! General Development
Hello!

I would like to have some params within my component's view.
I looked at:

http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_05
http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06

It's almost OK, but ...
There is an example only for one parameter. How can I get all of them?
I prepared default.xml file:


<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_MYCOMPONENT_VIEW_DEFAULT_TITLE">
<message>COM_MYCOMPONENT_VIEW_DEFAULT_DESC</message>
</layout>
<fields name="request"
addfieldpath="/administrator/components/com_mycomponent/models/
fields">
<fieldset name="request">
<field
name="id"
type="streamcategory"
label="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_LABEL"
description="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_DESC"
/>
</fieldset>


<fieldset name="request">
<field
name="limitFeatured"
type="list"
label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_LABEL"
description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</field>
</fieldset>

<fieldset name="request">
<field
name="limit"
type="list"
label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_LABEL"
description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</field>
</fieldset>
</fields>
</metadata>


How I can get those parameters in view?

Glenn Arkell

unread,
May 11, 2012, 5:55:42 AM5/11/12
to Joomla! General Development
Hi Grzesiek,

I have not mastered the menu option parameters myself as yet, but have
had some success with the overal component parameters. I think you
have an issue with the repeat of the same fieldset.
Try the following where you only have one fieldset of the 3 parameters
(id, limitFeatured, limit). This is a guess at this stage of my
understanding of parameters. Best of luck, cheers.

<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_MYCOMPONENT_VIEW_DEFAULT_TITLE">
<message>COM_MYCOMPONENT_VIEW_DEFAULT_DESC</message>
</layout>
<fields name="request"
addfieldpath="/administrator/components/
com_mycomponent/models/fields">
<fieldset name="request">
<field
name="id"
type="streamcategory"

label="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_DESC"
/>
<field
name="limitFeatured"
type="list"

label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</field>
<field
name="limit"
type="list"

label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
</field>
</fieldset>
</fields>
</metadata>

On May 11, 4:45 pm, Grzesiek <g.pa...@gmail.com> wrote:
> Hello!
>
> I would like to have some params within my component's view.
> I looked at:
>
> http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Compo...http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Compo...

Grzesiek

unread,
May 11, 2012, 6:17:30 AM5/11/12
to Joomla! General Development
I don't have any problems with params. You misunderstood me ;)

Params are saving well in the backend (Menu -> my component -> my
view).

No I would like to get them in site view :) And dunno how.

> >http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Compo......

Glenn Arkell

unread,
May 11, 2012, 6:27:09 AM5/11/12
to Joomla! General Development
Ok, so you have the params being set in the backend no problems, and
have managed to "gather" them via the model in the frontend, but just
want to use them in the view?

Grzesiek

unread,
May 11, 2012, 6:42:08 AM5/11/12
to Joomla! General Development
In link in first post I have an example, how can I get one parameter
from database or from config.xml file.
I don't know how to get (in model I think) them if I have for example
- 2 params from database and 2 from config.xml (static params).

With component's parameters we have simply way to get them:

$component = JComponentHelper::getComponent('com_mycomponent');
$params = $component->params;

But how to do this with view's params?

Glenn Arkell

unread,
May 11, 2012, 7:31:35 AM5/11/12
to Joomla! General Development
Ok, the config.xml file is for component overall parameters so your
config.xml should look something like this I think (with some reduced
options), which is slightly different from what you have above.

<?xml version="1.0" encoding="utf-8"?>
<config>
<fieldset name="params">
<field
name="id"
type="streamcategory"

label="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_TYPE_DESC"
/>
<field
name="limitFeatured"
type="list"

label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_FEATURED_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
.....
</field>
<field
name="limit"
type="list"

label="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_LABEL"

description="COM_MYCOMPONENT_FIELD_DISPLAY_LIMIT_DESC"
default="10"
>
<option value="5">5</option>
<option value="10">10</option>
.....
</field>
</fieldset>
</config>

Then in your view.html.php file you would have the following if you
needed to refer to the params there.

$app = JFactory::getApplication();
$params = $app->getParams();

And in the default.php under the view you would have something like
this to utilise the values in there.

// Get any parameters
$app = JFactory::getApplication();
$params = $app->getParams();
$limitFeatured = $params->get('limitFeatured', null);

This would allow you to use the value set in the parameter for
$limitFeatured. Hope this helps. Cheers.

Grzesiek

unread,
May 11, 2012, 7:45:30 AM5/11/12
to Joomla! General Development
Yay :) Thanks for you patience, but ...
That I know and this is working fine :)

I have problem with getting parameters specified in default.xml file
(site/views/tmpl/default.php/xml).
Like here:
http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_06

For one parameter, it's ok. But how to get more parameters? For
example I have 3, the first I'm getting without any problem, but I
don't have idea how to get the rest 2.
> ...
>
> więcej >>

Prasit Gebsaap

unread,
May 11, 2012, 10:00:31 AM5/11/12
to joomla-de...@googlegroups.com
Hi,

Here is how I handle menu item parameters (view parameters). I use this method as I want to override my global parameters with menu item parameters. I do this in model 's method.

    function populateState(){
        $component = JComponentHelper::getComponent( 'com_jongman' );
        $params = new JRegistry( $component->params );
       
        $menuitemid = JRequest::getInt( 'Itemid' );
        $menu = JSite::getMenu();
        if ($menuitemid) {
            $menuparams = $menu->getParams( $menuitemid );
            $params->merge( $menuparams );
        }
       
        $this->setState('params', $params);
    }


In view I just retreive it and assign for usage in layout

$this->state = $this->get("State");

In layout, do these:-

$params = $this->state->get("params");
$myParam1 = $params->get("myParam1", 1);
$myParam2 = $params->get("myParam2", 2);

Hope this help.



--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.




--
Prasit Gebsaap
 
Nonthaburi, Thailand
 

Grzesiek

unread,
May 11, 2012, 5:02:25 PM5/11/12
to Joomla! General Development
How do you retreiving params in view.html.php?
I'm trying to invoke $model->populateState(); but I'm getting an
error:

Fatal error: Call to protected method JModel::populateState() from
context 'gpLiveStreamViewIndex' in /stream/components/com_gplivestream/
views/index/view.html.php on line 20

Is this normal that I have all of my menu's parameters showing in URL
adress (&limit=10&cid=0, etc)?

On 11 Maj, 16:00, Prasit Gebsaap <prasit.gebs...@gmail.com> wrote:
> Hi,
>
> Here is how I handle menu item parameters (view parameters). I use this
> method as I want to override my global parameters with menu item
> parameters. I do this in model 's method.
>
>     function populateState(){
>         $component = JComponentHelper::getComponent( 'com_jongman' );
>         $params = new JRegistry( $component->params );
>
>         $menuitemid = JRequest::getInt( 'Itemid' );
>         $menu = JSite::getMenu();
>         if ($menuitemid) {
>             $menuparams = $menu->getParams( $menuitemid );
>             $params->merge( $menuparams );
>         }
>
>         $this->setState('params', $params);
>     }
>
> In view I just retreive it and assign for usage in layout
>
> $this->state = $this->get("State");
>
> In layout, do these:-
>
> $params = $this->state->get("params");
> $myParam1 = $params->get("myParam1", 1);
> $myParam2 = $params->get("myParam2", 2);
>
> Hope this help.
>
> On 11 May 2012 18:45, Grzesiek <g.pa...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Yay :) Thanks for you patience, but ...
> > That I know and this is working fine :)
>
> > I have problem with getting parameters specified in default.xml file
> > (site/views/tmpl/default.php/xml).
> > Like here:
>
> >http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Compo...
> ...
>
> więcej »

Glenn Arkell

unread,
May 11, 2012, 8:53:12 PM5/11/12
to Joomla! General Development
@ Grzesiek, As I mentioned I haven't mastered these params either so
I'm watching with great interest. Cheers.

@ Prasit, thanks for helping out here, I will be trying this at my
next opportunity. Much appreciated. Cheers.
> ...
>
> read more »

Mark Dexter

unread,
May 11, 2012, 9:05:25 PM5/11/12
to Joomla! General Development
Do it just the way Prasit indicates above. Your view.html.php should
extend JView. So you can use something like in the display() method:

$state = $this->get('State');

Then you can use something like:

$params = $state->get('params');

At this point, $params is a JRegistry object, so you can get your
parameter values by name:

$param1 = $params->get('param1', 'default');
$param2 = $params->get('param2', 'default');

Good luck. Mark
> ...
>
> read more »

Grzesiek

unread,
May 12, 2012, 8:04:28 AM5/12/12
to Joomla! General Development
Maybe I have something wrong in my menu:

From JSite::getMenu(); I'm getting for my ItemID this object:

[136] => stdClass Object ( [id] => 136 [menutype] => mainmenu [title]
=> LiveStream [alias] => livestream [note] => [route] => livestream
[link] => index.php?
option=com_gplivestream&view=index&cid=0&id=2&limitFeatured=20&limit=30
[type] => component [level] => 1 [language] => * [browserNav] => 0
[access] => 1 [params] => JRegistry Object ( [data:protected] =>
stdClass Object ( [menu-anchor_title] => [menu-anchor_css] =>
[menu_image] => [menu_text] => 1 [page_title] => [show_page_heading]
=> 0 [page_heading] => [pageclass_sfx] => [menu-meta_description] =>
[menu-meta_keywords] => [robots] => [secure] => 0 [gk_showtitle] => 1
[gk_desc] => [gk_cols] => 1 [gk_group] => 0 [gk_class] =>
[gk_subcontent] => 0 [gk_subcontent_pos_positions] => menu1 ) ) [home]
=> 0 [img] => [template_style_id] => 0 [component_id] => 10019
[parent_id] => 1 [component] => com_gplivestream [tree] => Array ( [0]
=> 136 ) [query] => Array ( [option] => com_gplivestream [view] =>
index [cid] => 0 [id] => 2 [limitFeatured] => 20 [limit] => 30 ) )

And from Your code I'm getting only:
blablablaJRegistry Object ( [data:protected] => stdClass Object
( [menu-anchor_title] => [menu-anchor_css] => [menu_image] =>
[menu_text] => 1 [page_title] => [show_page_heading] => 0
[page_heading] => [pageclass_sfx] => [menu-meta_description] => [menu-
meta_keywords] => [robots] => [secure] => 0 [gk_showtitle] => 1
[gk_desc] => [gk_cols] => 1 [gk_group] => 0 [gk_class] =>
[gk_subcontent] => 0 [gk_subcontent_pos_positions] => menu1 ) )

Scared ...
> ...
>
> więcej »

elin

unread,
May 12, 2012, 12:17:40 PM5/12/12
to joomla-de...@googlegroups.com
No it's not really normal to have all of those in your request. Usually you would just have the id or in the case of search the search terms (if any) or in the case of featured content, nothing and you'd save those items in fields. 

Incidentally I would make those fields type="integer" with the appropriate options  and filter="options" for safety. 

Are you talking about params stored in their own fields or stored in the param field?

Why are you trying to populate the state in the view? Is it really the view's job to do that in your mind?

How are you doing the parameter merge?

Elin

Glenn Arkell

unread,
May 12, 2012, 5:46:43 PM5/12/12
to joomla-de...@googlegroups.com
For what it's worth, I've followed the suggested steps from Prasit in the Helloworld MVC tutorial (step 18) and it works fine.  I didn't do any parameter merging, just set these params separately to any component parameters.  I will add a new (19) step to the doco soon as an Example of using parameters (and stylesheets).  Here are the snippets of code used.

Model - updhelloworld.php (just the getitem function)
    function &getItem()
    {

        if (!isset($this->_item))
        {
            $cache = JFactory::getCache('com_helloworld', '');
            $id = $this->getState('helloworld.id');
            $this->_item =  $cache->get($id);
            if ($this->_item === false) {

                // Menu parameters
                $menuitemid = JRequest::getInt( 'Itemid' );  // this returns the menu id number so you can reference parameters

                $menu = JSite::getMenu();
                if ($menuitemid) {
                   $menuparams = $menu->getParams( $menuitemid );
                   $headingtxtcolor = $menuparams->get('headingtxtcolor');  // This shows how to get an individual parameter for use
                   $headingbgcolor = $menuparams->get('headingbgcolor');  // This shows how to get an individual parameter for use
                }
                $this->setState('menuparams', $menuparams);  // this sets the parameter values to the state for later use
            }
        }
        return $this->_item;

    }

View - view.html.php (in the display function) just add

        $this->state = $this->get('State');


View - default.php
// get the menu parameters for use
$menuparams = $this->state->get("menuparams");
$headingtxtcolor = $menuparams->get("headingtxtcolor");
$headingbgcolor = $menuparams->get("headingbgcolor");

and then use the above variables in some way...
    <h2 style="color:<?php echo $headingtxtcolor; ?>; background-color:<?php echo $headingbgcolor; ?>;">Update the Hello World greeting</h2>

View - default.xml (define the parameters)
    <fields name="params">
        <fieldset name="request">
            <field name="headingtxtcolor" type="text" default="#ff0000" size="40"
                label="COM_HELLOWORLD_UPDHELLOWORLD_FIELD_HEADINGTXTCOLOR_LABEL"
                description="COM_HELLOWORLD_UPDHELLOWORLD_FIELD_HEADINGTXTCOLOR_DESC" />
            <field name="headingbgcolor" type="text" default="#0000ff" size="40"
                label="COM_HELLOWORLD_UPDHELLOWORLD_FIELD_HEADINGBGCOLOR_LABEL"
                description="COM_HELLOWORLD_UPDHELLOWORLD_FIELD_HEADINGBGCOLOR_DESC" />
        </fieldset>
    </fields>


Works like a charm.  Hope this helps.  Cheers.

elin

unread,
May 13, 2012, 12:27:04 PM5/13/12
to joomla-de...@googlegroups.com
I was asking Grzesik but if you want to have the global/article/menu cascade of parameters to work as expected you do need to do a parameter merge. 
You can certainly do a lot on the request if that's what you want, but I think the core components work like a charm too and you might want to follow their design. Since it's not uncommon to want to have 30 or more parameters it's going to get pretty intense to always have to set them via a menu item 

In Part 19 for reasons I don't get there is a second fieldset called request being stored inside the JSON string of the params field.   I'm not sure why you would do that when you can get them directly from the params just like everything else.


shows how to do the merge of item, menu and global parameters. I strongly encourage people to use the core components as models. 

Elin

Glenn Arkell

unread,
May 13, 2012, 9:22:23 PM5/13/12
to joomla-de...@googlegroups.com
Hi Elin,

Apologies, I should have directed my response to Grzesik, it wasn't meant to be a response to your input.

As for my Part 19 attempt at providing some further examples around params, as my comment at the top indicates I'm very much a learner in the world of Joomla! so I appreciate your pointers to further my knowledge.  If I can ask some further "probably silly questions" - when you say second fieldset... I only have one fieldset in the default.xml file?  So I probably have a lack of understanding around "JSON" as a whole.  I will keep reading and listening and again I appreciate all the input you and the other gurus provide on this loop.  Cheers.

Glenn

elin

unread,
May 14, 2012, 6:10:18 AM5/14/12
to joomla-de...@googlegroups.com
If you followed the instructions in the tutorial you made a request field set in part 5. Then you added a second one in part 19 that is inside the params fields tags.
That's what I am talking about.

Elin

Glenn Arkell

unread,
May 15, 2012, 9:36:15 PM5/15/12
to joomla-de...@googlegroups.com
Hi Elin,
Thanks for responding.  I think I must be missing something then because I thought that I would have to have separate parameters for very separate functions.  Ie... Part 5 is about parameters for the basic "listing" of the hellow world message, whereas my Part 19 is about a new front-end update function to enable a menu option to show the messages stored in the db that can be updated.  I will keep reading and listening and one day I might get it ;)  Cheers.

Glenn
Reply all
Reply to author
Forward
0 new messages