Get URL from a Menu Item ID

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Get URL from a Menu Item ID

Post by stickey » Mon Jul 06, 2009 4:11 pm

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?

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Get URL from a Menu Item ID

Post by ianmac » Mon Jul 06, 2009 5:37 pm

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?
It depends on what you want the URL for...

Ian

User avatar
C10ne
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Wed Sep 10, 2008 3:46 pm
Contact:

Re: Get URL from a Menu Item ID

Post by C10ne » Mon Jul 06, 2009 5:40 pm

I believe that puling the item from the menu table.

Code: Select all

$sql = "SELECT link FROM #__menu WHERE id = ".$myItmId;

$db->SetQuery($query);
$menuItem = $db->loadObject();

$link = JRoute::_($menuItem->link);
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.
Destruction is the mother of creativity

stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Re: Get URL from a Menu Item ID

Post by stickey » Mon Jul 06, 2009 5:49 pm

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,

Code: Select all

  
5 $application = JFactory::getApplication();
6 $menu = $application->getMenu();
7 $item = $menu->getItem( ID );
From there I can access $item->link and run it through JRoute::_

Can anyone verify?

ajith
Joomla! Apprentice
Joomla! Apprentice
Posts: 24
Joined: Fri Jun 26, 2009 5:59 am

Re: Get URL from a Menu Item ID

Post by ajith » Mon Jul 06, 2009 5:52 pm

Hi

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

stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Re: Get URL from a Menu Item ID

Post by stickey » Mon Jul 06, 2009 6:24 pm

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.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: Get URL from a Menu Item ID

Post by ianmac » Mon Jul 06, 2009 6:37 pm

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.
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.

This is how it is done in the rest of the CMS.

Ian

stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Re: Get URL from a Menu Item ID

Post by stickey » Mon Jul 06, 2009 7:10 pm

Thanks for your help Ian. I will go ahead and append the ItemID onto the end of the string.

User avatar
lcdweb
Joomla! Intern
Joomla! Intern
Posts: 87
Joined: Thu Jul 19, 2007 3:32 am
Location: Albany, NY
Contact:

Re: Get URL from a Menu Item ID

Post by lcdweb » Thu Sep 03, 2009 3:44 am

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.

petsoukos
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Tue Sep 01, 2009 2:04 pm
Contact:

Re: Get URL from a Menu Item ID

Post by petsoukos » Fri Sep 18, 2009 11:47 am

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.

stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Re: Get URL from a Menu Item ID

Post by stickey » Fri Sep 18, 2009 3:27 pm

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.

petsoukos
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Tue Sep 01, 2009 2:04 pm
Contact:

Re: Get URL from a Menu Item ID

Post by petsoukos » Fri Sep 18, 2009 3:32 pm

That would be great my friend!

stickey
Joomla! Apprentice
Joomla! Apprentice
Posts: 33
Joined: Wed Mar 18, 2009 4:17 pm

Re: Get URL from a Menu Item ID

Post by stickey » Fri Sep 18, 2009 3:44 pm

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.

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 );
I think that should work - haven't tested the code however.

petsoukos
Joomla! Intern
Joomla! Intern
Posts: 53
Joined: Tue Sep 01, 2009 2:04 pm
Contact:

Re: Get URL from a Menu Item ID

Post by petsoukos » Sat Sep 19, 2009 11:21 am

Thanks. I can set the form action to include the Itemid.

Vahan
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Oct 01, 2009 9:41 am

Re: Get URL from a Menu Item ID

Post by Vahan » Thu Oct 01, 2009 10:20 am

after a lot of searching, obviously starting here, this is what i found to work perfectly (assuming the ItemID is in the variable $menuitem):
$item = JFactory::getApplication()->getMenu()->getItem( $menuitem );
$url = JRoute::_($item->link . '&Itemid=' . $item->id);
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.

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" />
inside your module's php file, have this:

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>";
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

jonasfh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 213
Joined: Mon Dec 18, 2006 11:07 pm

Re: Get URL from a Menu Item ID

Post by jonasfh » Wed Apr 27, 2011 2:06 pm

Hi,
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}"); 
...which makes sense: when you have the menu id, it should be an easy task to get the link to that menuitem...

regards Jonas
--
Jonas at arcgiraff.no

User avatar
svempa
Joomla! Apprentice
Joomla! Apprentice
Posts: 49
Joined: Thu Oct 12, 2006 9:07 am
Location: Karlstad, Sweden

Re: Get URL from a Menu Item ID

Post by svempa » Mon Sep 05, 2011 11:58 am

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. :)

Ometha
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Jul 28, 2011 8:30 am

Re: Get URL from a Menu Item ID

Post by Ometha » Fri Nov 11, 2011 5:55 pm

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

crunch483
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Oct 25, 2011 3:12 pm

Re: Get URL from a Menu Item ID

Post by crunch483 » Thu Nov 29, 2012 8:15 pm

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:

Code: Select all

JRoute::_('index.php?option=com_easyblog&Itemid=486');
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).

lahmizzar
Joomla! Fledgling
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

Post by lahmizzar » Tue May 28, 2013 4:12 am

jonasfh wrote:Hi,
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}"); 
...which makes sense: when you have the menu id, it should be an easy task to get the link to that menuitem...

regards Jonas

Code: Select all

$itemid=10; 
echo JRoute::_('index.php?Itemid=' . $itemid); 
Its just the old 1.5 here, but this works for me too in joomla 3.0, 3.1


Locked

Return to “Joomla! 1.5 Coding”