Get URL from a Menu Item ID
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Get URL from a Menu Item ID
How do I get a full URL from only having a menu item id via the API? I've been looking through JRouter and JRoute - as well as mod_mainmenu and can't seem to come up with the solution.
I would like to be able to derive a full URL from only knowing a menu item ID. Seems like it should be possible - but perhaps not.
Is it as simple as pulling the "link" column from the jos_menu table?
I would like to be able to derive a full URL from only knowing a menu item ID. Seems like it should be possible - but perhaps not.
Is it as simple as pulling the "link" column from the jos_menu table?
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: Get URL from a Menu Item ID
It depends on what you want the URL for...stickey wrote:How do I get a full URL from only having a menu item id via the API? I've been looking through JRouter and JRoute - as well as mod_mainmenu and can't seem to come up with the solution.
I would like to be able to derive a full URL from only knowing a menu item ID. Seems like it should be possible - but perhaps not.
Is it as simple as pulling the "link" column from the jos_menu table?
Ian
- C10ne
- Joomla! Apprentice
- Posts: 21
- Joined: Wed Sep 10, 2008 3:46 pm
- Contact:
Re: Get URL from a Menu Item ID
I believe that puling the item from the menu table.
Maybe some of the experts can offer a better solution, but as far as i have addressed the matter this is the only way i've found.
Code: Select all
$sql = "SELECT link FROM #__menu WHERE id = ".$myItmId;
$db->SetQuery($query);
$menuItem = $db->loadObject();
$link = JRoute::_($menuItem->link);
Destruction is the mother of creativity
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Re: Get URL from a Menu Item ID
I need it as a string to put into an HREF attribute of an A element of an XHTML override of a module.
But i think i got it sorted out,
From there I can access $item->link and run it through JRoute::_
Can anyone verify?
But i think i got it sorted out,
Code: Select all
5 $application = JFactory::getApplication();
6 $menu = $application->getMenu();
7 $item = $menu->getItem( ID );
Can anyone verify?
-
- Joomla! Apprentice
- Posts: 24
- Joined: Fri Jun 26, 2009 5:59 am
Re: Get URL from a Menu Item ID
Hi
Please use this code
$menu = & JSite::getMenu();
$item = $menu->getActive();
$id=$item->itemid;
It will be take current menu Item id
Please use this code
$menu = & JSite::getMenu();
$item = $menu->getActive();
$id=$item->itemid;
It will be take current menu Item id
http://www.joomla-web-developer.com Joomla Developer, Joomla Development
http://www.joomla-web-designer.com Joomla Designer, Joomla Design
http://www.joomla-web-designer.com Joomla Designer, Joomla Design
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Re: Get URL from a Menu Item ID
I'm not getting the ItemID on the URL like expected with my code above - not previous posters. Anyone know how to ensure that I get the ItemID as well? I'd rather not just tag it onto the end of the string. I'd like to use the API to do the work for me.
- ianmac
- Joomla! Virtuoso
- Posts: 4784
- Joined: Sat Sep 24, 2005 11:01 pm
- Location: Toronto, Canada
Re: Get URL from a Menu Item ID
No, it won't give you the link with the Itemid. The proper way is to append the Itemid yourself if that is what you need to do.stickey wrote:I'm not getting the ItemID on the URL like expected with my code above - not previous posters. Anyone know how to ensure that I get the ItemID as well? I'd rather not just tag it onto the end of the string. I'd like to use the API to do the work for me.
This is how it is done in the rest of the CMS.
Ian
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Re: Get URL from a Menu Item ID
Thanks for your help Ian. I will go ahead and append the ItemID onto the end of the string.
- lcdweb
- Joomla! Intern
- Posts: 87
- Joined: Thu Jul 19, 2007 3:32 am
- Location: Albany, NY
- Contact:
Re: Get URL from a Menu Item ID
stickey --
I'm looking to do this too, but seem to be missing something from the suggestions provided. Can you post back your final code? given ItemID, how do I retrieve the full url to the menu item.
I'm looking to do this too, but seem to be missing something from the suggestions provided. Can you post back your final code? given ItemID, how do I retrieve the full url to the menu item.
-
- Joomla! Intern
- Posts: 53
- Joined: Tue Sep 01, 2009 2:04 pm
- Contact:
Re: Get URL from a Menu Item ID
I'm searching for the same thing too. I'm making a component for educational purpose. This component has a submission form in the Frond-End. It's like a search but with Dates (e.g. 2009-09-11). This form uses the POST method but I'm losing the ItemID from the URL and with it the Modules in the same page.
<form action="index.php?option=com_comp" method="post">
There is probably a way to get the form action="" from the API that attaches the itemid... but can't find it.
<form action="index.php?option=com_comp" method="post">
There is probably a way to get the form action="" from the API that attaches the itemid... but can't find it.
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Re: Get URL from a Menu Item ID
Sorry to not get back to anyone here. If I remember I just grabbed the ItemID from the QueryString or the POST variables and appended onto the end of the URL I was building. I'll see if I can't dig up the code I used. I should have posted here when I got my solution before - sorry.
-
- Joomla! Intern
- Posts: 53
- Joined: Tue Sep 01, 2009 2:04 pm
- Contact:
Re: Get URL from a Menu Item ID
That would be great my friend!
-
- Joomla! Apprentice
- Posts: 33
- Joined: Wed Mar 18, 2009 4:17 pm
Re: Get URL from a Menu Item ID
So I couldn't find my exact code - however I think I remember what I did.
Given an ItemID you can pull the link column from jos_menu. From there you init a JURI object and call setVar with the ItemID. This will append the ItemID onto the end of the link.
Code would be something like this - end result is JURI object which you can then do whatever you need.
I think that should work - haven't tested the code however.
Given an ItemID you can pull the link column from jos_menu. From there you init a JURI object and call setVar with the ItemID. This will append the ItemID onto the end of the link.
Code would be something like this - end result is JURI object which you can then do whatever you need.
Code: Select all
$itemid = JRequest::getVar( 'Itemid' );
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem( $itemid );
$link = new JURI($item->link);
$link->setVar( 'ItemId', $itemid );
-
- Joomla! Intern
- Posts: 53
- Joined: Tue Sep 01, 2009 2:04 pm
- Contact:
Re: Get URL from a Menu Item ID
Thanks. I can set the form action to include the Itemid.
-
- Joomla! Fledgling
- Posts: 1
- Joined: Thu Oct 01, 2009 9:41 am
Re: Get URL from a Menu Item ID
after a lot of searching, obviously starting here, this is what i found to work perfectly (assuming the ItemID is in the variable $menuitem):
to make a module with this, just do the following
inside your module's xml file, have these two parameters:
inside your module's php file, have this:
also, if you really want to save some space you can collapse iton this one line:
$url = JRoute::_(JFactory::getApplication()->getMenu()->getItem( $menuitem )->link . "&Itemid=$menuitem");
and dont forget to change the bold bits to fit your mod
you need to get the link AND the id from the menu item object and add the id on manually before JRoute can translate it.$item = JFactory::getApplication()->getMenu()->getItem( $menuitem );
$url = JRoute::_($item->link . '&Itemid=' . $item->id);
to make a module with this, just do the following
inside your module's xml file, have these two parameters:
Code: Select all
<param name="text" type="text" default="My Link." label="Text" description="Text to be displayed." size="40" />
<param name="menuitem" type="menuitem" default="1" label="Menu link" description="Select a menu item" />
Code: Select all
$text = $params->get( 'text' );
$menuitem = $params->get( 'menuitem' );
$item = JFactory::getApplication()->getMenu()->getItem( $menuitem ); // get menu item object from menu item id
$url = JRoute::_($item->link . "&Itemid=" . $item->id); // use JRoute to make link from object
echo "<a href='$url'>$text</a>";
$url = JRoute::_(JFactory::getApplication()->getMenu()->getItem( $menuitem )->link . "&Itemid=$menuitem");
and dont forget to change the bold bits to fit your mod
-
- Joomla! Enthusiast
- Posts: 213
- Joined: Mon Dec 18, 2006 11:07 pm
Re: Get URL from a Menu Item ID
Hi,
not sure if JRoute is getting smarter over the years, but this seems to work fine for me:
...which makes sense: when you have the menu id, it should be an easy task to get the link to that menuitem...
regards Jonas
not sure if JRoute is getting smarter over the years, but this seems to work fine for me:
Code: Select all
$itemid=10;
echo JRoute::_("index.php?Itemid={$itemid}");
regards Jonas
--
Jonas at arcgiraff.no
Jonas at arcgiraff.no
- svempa
- Joomla! Apprentice
- Posts: 49
- Joined: Thu Oct 12, 2006 9:07 am
- Location: Karlstad, Sweden
Re: Get URL from a Menu Item ID
Thanks for the post, Vahan. I had worked out most of it, but didn't know about the necessity of Itemid for the url to get translated correctly on different views.
-
- Joomla! Apprentice
- Posts: 17
- Joined: Thu Jul 28, 2011 8:30 am
Re: Get URL from a Menu Item ID
Hi,
how can i get the item id of a menu item for manipulating the readmore links? the reason i need that is that I have a menu item which displays featured articles in my page and some of my featured articles on their own have been linked to another featured articles menu item. So, I want to make a readmore link directed to the menu item instead of the article link to be able to get those featured articles all in that menu item, not the article itself only. Please guide me how this is possible.
Thanks
Ometha
how can i get the item id of a menu item for manipulating the readmore links? the reason i need that is that I have a menu item which displays featured articles in my page and some of my featured articles on their own have been linked to another featured articles menu item. So, I want to make a readmore link directed to the menu item instead of the article link to be able to get those featured articles all in that menu item, not the article itself only. Please guide me how this is possible.
Thanks
Ometha
-
- Joomla! Fledgling
- Posts: 1
- Joined: Tue Oct 25, 2011 3:12 pm
Re: Get URL from a Menu Item ID
Thanks jonasfh, that helped me out! Using just index.php?itemID=[item ID here] I was finally able to get the same SEF URL used by the menu item I wanted. Prior to doing that I was getting some extraneous parameters at the end of the URL that weren't on the menu link (in my case, resources/blog/entry?layout=latest instead of just resources/blog).
The final version I ended up with was:
which was the best solution in this case because even if the menu item is deleted and the Itemid is no longer valid, it will still link to the component (better than nothing, although it still might not link to the correct view - I'm not sure how to resolve that, but this is sufficient for my current purposes).
The final version I ended up with was:
Code: Select all
JRoute::_('index.php?option=com_easyblog&Itemid=486');
-
- Joomla! Fledgling
- Posts: 2
- Joined: Tue May 28, 2013 3:47 am
Get URL from a Menu Item ID in Joomla 3, 3.0, 3.1
jonasfh wrote:Hi,
not sure if JRoute is getting smarter over the years, but this seems to work fine for me:...which makes sense: when you have the menu id, it should be an easy task to get the link to that menuitem...Code: Select all
$itemid=10; echo JRoute::_("index.php?Itemid={$itemid}");
regards Jonas
Code: Select all
$itemid=10;
echo JRoute::_('index.php?Itemid=' . $itemid);