Dealing with ItemID

165 views
Skip to first unread message

tampe125

unread,
Dec 14, 2011, 10:42:27 AM12/14/11
to joomla-de...@googlegroups.com
hi, i have a question about the ItemdID.

as far as i know, Joomla! gets the ItemdID from the current page or the active menu. But in this way sometimes you can have "odd" results.
Let me explain with an example:

let's say i have two menu entries (on frontend):

- Item list
- Add a new item

I create a new item and then i save it. On my controller i should have at the end of save() task this line:

$this->setRedirect(URL_TO_MY_ITEM_LIST);

well, after doing that, on the new page you'll see the item list, but the "Add a new item" menu is still active!

How can i deal with that? I'm the only one?

thanks in advance!

Swapnil Shah

unread,
Dec 14, 2011, 5:04:29 PM12/14/11
to joomla-de...@googlegroups.com
Okay, I can't quite figure out where you are having the problem. 

Are you using jroute to create the URL to your item-list?

You can pass jroute an Itemid and it will not look for one. If you don't pass one in, it will look for one for that component that matches or use the default Itemid. 

In your post, you are telling joomla to display your item-list. But state that add a new item is active? How do you know it's still active? By styling of mod_menu? Or the page styling?

Regards, 


Neil
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/TvazGSvf1r4J.
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.

tampe125

unread,
Dec 15, 2011, 5:14:58 AM12/15/11
to joomla-de...@googlegroups.com
Yep, i'm using JRoute; it's not a real issue, it's just an annoying thing :)

i'll try to explain myself better.
Again the two menu example:

- Item list (url:index.php?option=my_comp&view=items&Itemid=100)
- Add new item (url:index.php?option=my_comp&view=item&layout=edit&Itemid=200)

Let's say i surf on "Add new Item", add it, and then i save it.
On my mind, i should go back to the item list page, and i should see the same page.

But what happens?
this is the result of jrouting this link:

$link = JRoute::_('index.php?option=my_comp&view=items')

//link == 'index.php?option=my_comp&view=items&Itemid=200'

this means that, even if i'm looking at the item list, i have the "Add new item" menù active, so Joomla would display the modules associated to the menù!
I found two solutions:

- Who cares? :P
- Save the itemid and manually add it to the link

I'd wish to know if it's a "known thing" or if i'm doing something wrong.
Cheers!

brian teeman

unread,
Dec 15, 2011, 5:22:39 AM12/15/11
to joomla-de...@googlegroups.com
As far as I remember if no itemid is explicitly set then the itemid of the referring (parent) link is used by default

elin

unread,
Dec 15, 2011, 7:58:00 AM12/15/11
to joomla-de...@googlegroups.com
Does the item list have its own itemId (direct menu link) or is it getting that 100  from somewhere else? 

Elin

tampe125

unread,
Dec 15, 2011, 8:52:46 AM12/15/11
to joomla-de...@googlegroups.com
both "Item list" and "Add new item" are menu item, so they have their own itemid

elin

unread,
Dec 15, 2011, 11:00:55 AM12/15/11
to joomla-de...@googlegroups.com
$link = JRoute::_('index.php?option=my_comp&view=items')   This link doesn't have a category id or something associated with it?

Elin

tampe125

unread,
Dec 15, 2011, 11:11:55 AM12/15/11
to joomla-de...@googlegroups.com
no.
btw i'm doing a simple example, just to explain the situation.

maybe a solution could be to query the db and found if the link is used in any menu...

Swapnil Shah

unread,
Dec 15, 2011, 12:33:29 PM12/15/11
to joomla-de...@googlegroups.com
That's weird. Haven't looked at JRoute in a while, but I am pretty sure it looks through JMenu to find a match. 

I suppose you could do that manually. Try using JMenu to find your menu item. I personally don't like to hardcore things. 

You don't have to query the DB, just have JFactory get you JMenu. That way you only get items that the current user has access too. 

Just had a thought as I typed the last statement, check the ACL, make sure access is set appropriately. If there is a permission conflict, jroute won't be able to find it. Which is the same problem you will have unless you query the db directly. 

Regards, 


Neil
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/FOFgVHliOMIJ.

tampe125

unread,
Dec 22, 2011, 4:48:37 AM12/22/11
to joomla-de...@googlegroups.com
hi, i have done some tests, and JRoute adds the current menu item, not the correct one (ie the one that matches with the link).

so, i have created a function that searches for the correct item id.
this is my code, where can i put it? at the moment i didn't exactly know who deals with itemid ;(

    function getItemid($url)
    {
        //i use the route file to split the url and "clean" it from appended vars 
        $app    = JFactory::getApplication();
        $groups    = JFactory::getUser()->getAuthorisedViewLevels();
        $menu   = $app->getMenu();
        $router    = $app->getRouter();

        $parts = $router->build($url)->get('_vars');

        //build up the "cleaned" url
        $search  = 'index.php?option='.$parts['option'];
        $search .= $parts['view']     ? '&view='.$parts['view']         : '';
        $search .= $parts['layout'] ? '&layout='.$parts['layout']     : '';

        //is this url a menu link?
        foreach($menu->getMenu() as $key => $item)
        {
            if($item->link == $search && in_array($item->access, $groups))
            {
                $itemid = $key;
                break;
            }
        }
        //mhm not a menu link, let's get the current one
        if(!$itemid)
        {
            $app     = JFactory::getApplication();
            $menu     = $app->getMenu();
            $itemid = $menu->getActive()->id;
        }

        //uh oh, got no itemid, let's try with JRequest
        if(!$itemid) $itemid = JRequest::getInt('Itemid');

        return $url .= '&Itemid='.$itemid;
    }

Sam Moffatt

unread,
Dec 23, 2011, 1:20:28 PM12/23/11
to joomla-de...@googlegroups.com
The "correct" Itemid is subjective in our system because you can have
many menu items pointing to the same place.

Also searching through the entire menu is an O(n) operation which
means that you're loading the entire menu and taking much of that RAM
to look for something that may or may not exist. What happens if you
have 40 menus and 20k items?

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.
> To view this discussion on the web, visit

> https://groups.google.com/d/msg/joomla-dev-general/-/v3eKgGE3-qEJ.

tampe125

unread,
Dec 23, 2011, 3:24:57 PM12/23/11
to joomla-de...@googlegroups.com
well, if think there could be very very few cases where the itemids are more than 200.
however, i can replace the for statement whith a query.

Sam Moffatt

unread,
Dec 23, 2011, 9:37:37 PM12/23/11
to joomla-de...@googlegroups.com
I think a site with 200 is actually reasonably small in the grand
scheme of things. The site I work on day to day has 207 entries in the
menu table and I think it's actually quite small. I'm sure there are
plenty of people out here that have sites with more than 200 entries
and I just another larger Joomla! site I know and got to this:
http://www.toowoombarc.qld.gov.au/?Itemid=1521

While not all menu items might not be active and could be deleted, I
think we can safely assume it has more than 200.

Again, "correct" is subjective in our routing model, the router cannot
safely guess the "correct" item ID because multiple item ID's might be
available OR it might be part of a more complicated flow (category
pages).

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.
> To view this discussion on the web, visit

> https://groups.google.com/d/msg/joomla-dev-general/-/LFtO4Ped8tYJ.

Herman Peeren

unread,
Dec 24, 2011, 3:11:07 AM12/24/11
to Joomla! General Development
Have you got a router.php in your component? There the correct Itemid
is resolved. You can do that with getMenu(), like everywhere in Joomla
(look at the router.php of com_content for instance).

Untested, just came to my mind: you could try if you can just "reset"
the Itemid in your redirect by adding "&Itemid=" (without a value) to
your URL.

If this is a custom component that is only to be used on a certain
site, then you can of course know in advance if the number of menu-
items is limited to 200. If the component is for general use, then
not. But then still I would not mind much about a linear order, not
even with 20000 menuitems as suggested earlier (which seems to me
rather exceptional BTW): when running a site that size you should look
after a server with enough memory and power. And as said: the core
Joomla router.php files are loading the entire menu in memory too!

In the given case (!) I don't understand what Sam means with:"correct"
is subjective in our routing model. In this example there is only 1
menu-item, so 1 Itemid for the list of items. Or am I overlooking
something?

Bottomline, I'd say: router.php for your component.

Sam Moffatt

unread,
Dec 24, 2011, 3:35:54 AM12/24/11
to joomla-de...@googlegroups.com
Consider the following innocuous made up URL:
index.php?option=com_content&view=article&id=1

This humble URL can have more than one Itemid in different parts of
the site with different assigned modules and templates. Just create
two menu items pointing to the same content. At that point a given
link, which is what is being searched for here, would have more than
one Itemid - it then becomes impossible to determine which one is the
"correct" one.

Other examples include traversing through a category blog or list view
into an article view. Our current model retains the item ID so that
the menu, template and what not remain consistent. Presuming said item
had an entry in the menu system (which it may not necessarily have,
again you can legitimately NOT have an Itemid for a given URL), it
could be linked from another part of the site with completely
different modules and may even a different template. In this
particular case would it be desirable to pick up the "correct" Itemid
and use it's params or should it continue down the pathway of the
logical requestor's Itemid that was used to get the category list?

In this way it is subjective as the "correct" Itemid is based on how
you got to where you are and potentially where you want to go. And in
some cases an Itemid might not exist at all.

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.

Herman Peeren

unread,
Dec 24, 2011, 4:02:35 AM12/24/11
to Joomla! General Development
In the example, first posting in this thread, it was given: 1 menu-
item for the list of items. No ambiguity here. So, I understand the
examples Sam is giving, but that is not the question IMHO.

I think the question here is: how do we, in this case, get rid of the
item ID that is retained by the system or how do we best replace it
with the item ID we want (without hardcoding it). The latter is done
in the earlier posting with the getItemid($url)-function. My
suggestion is that such code belongs in router.php of the component.

tampe125

unread,
Dec 24, 2011, 4:06:59 AM12/24/11
to joomla-de...@googlegroups.com
thanks herman, i think that's the right place.

however, i understand the problem of the "correct" Itemid, infact i was thinking to add the function i wrote as an "helper" in 3rd part extensions.
if you need that, you use it, otherwise ignore it :)

Sam Moffatt

unread,
Dec 24, 2011, 6:33:01 AM12/24/11
to joomla-de...@googlegroups.com
The problem is that you can't make the assumption that there will only
ever be one of those items…unless you're building this for a specific
site. And even then don't underestimate the ability for your end user
to utilise the power of the system you've given them to create their
own menu item. You can hope that someone doesn't do something
unexpected but what happens when someone does?

What happens when they have two list links and are located distinctly
within the app and they click "add" in one section and then upon
saving are sent to a list page elsewhere (perhaps with no add button
in that menu). You end up with a greater problem because at this point
instead of the wrong thing being highlighted, the user is potentially
in the wrong place…but it is also the right place…triggering question
"where is the add item link?". Short of either adding another add link
or deleting that list link, those people trying to front end
administrate the site could be set up to a frustrating user experience
("why can't it just go back to where it was? why does it have to jump
around?").

As an alternative if two menu items are indeed paired, have the list
view menu item set as a param for the add item view and then just add
the Itemid based on the params for the menu item. The params are
already loaded and easily available in the component saving you a look
up of the menu tree.

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.

> To view this discussion on the web, visit

> https://groups.google.com/d/msg/joomla-dev-general/-/dR6w5LliooAJ.

elin

unread,
Dec 24, 2011, 9:49:24 AM12/24/11
to joomla-de...@googlegroups.com
The About Joomla menu in sample data has 81 so ... I would say 200 while probably more than most sites is still more in the medium range of things.
One reason people have a lot of menu links is so that they can precisely control their urls without falling back on the menus where items are located for the convenience of site visitors or because they want to really control modules, templates and layouts.  Lots of people I know make a complete set of links for a menu never displayed to users that structures their urls as desired and then make their public facing menus.

I think the router stops at the first match of itemId (if present) or then option, view, id. So while there may be many links to an item unless they have an Itemid that you are referencing you are never going to get to them. 


Do you think we've been pining  UCM for long enough?

Elin

tampe125

unread,
Dec 24, 2011, 10:53:02 AM12/24/11
to joomla-de...@googlegroups.com
so, to sum up, here it is the situation:
  • atm Joomla! saves the current itemid; so, sometimes, when you redirect, you can have an active menu that's different from the content (ie "Add new item" menu active while you're showing the "Item list")
  • using the previous function you can try to "guess" the correct itemid
  • sadly, we cannot use it since, sometimes, users refers the same view on multiple menu
  • in conclusion, i think there is no "elegant" solution: you can only save the menu id as param, then on your component use it to show the desired menu

Did i miss something?

Sam Moffatt

unread,
Dec 24, 2011, 6:12:04 PM12/24/11
to joomla-de...@googlegroups.com
On Sat, Dec 24, 2011 at 7:53 AM, tampe125 <tamp...@gmail.com> wrote:
> so, to sum up, here it is the situation:
>
> atm Joomla! saves the current itemid; so, sometimes, when you redirect, you
> can have an active menu that's different from the content (ie "Add new item"
> menu active while you're showing the "Item list")

In a sense that's also a feature as well as a PITA. Really works both
ways. Though for you it works negatively.

> using the previous function you can try to "guess" the correct itemid
> sadly, we cannot use it since, sometimes, users refers the same view on
> multiple menu

Yeah, which is a flaw of our menu model that also means you can easily
created duplicate content. Of course, this is in a sense a feature as
well by being able to structure stuff in different ways and have
different keywords, pathways and entry points to the same content.

> in conclusion, i think there is no "elegant" solution: you can only save the
> menu id as param, then on your component use it to show the desired menu

That's probably the best of a series of bad options.

>
> Did i miss something?

Nope, a good succinct summary :)

Cheers,

Sam

Reply all
Reply to author
Forward
0 new messages