7

I am developing my own slideshow component and module for Joomla. The component is used to select the slides along with captions and links etc. The module shows the slides.

In the component, for each slide I have enabled the option to add a link to each slide. The field type that I have used is the menu item field type http://docs.joomla.org/Menuitem_form_field_type

This seems to add the menu item id to the database for that particular slide.

My question is; how do I take this menu item id and then in the module use it to get the full menu item link? I will understand that I will probably have to search through the database to find the right menu item and then find the appropriate link but I am not quite sure where to start.

If anyone could point me in the right direction I would appreciate it.

Thanks :)

2 Answers 2

5

To get the ID which I assume you know already, you can use the following:

$itemid = $params->get('field_name');

To get the URL based on the ID, try this:

$itemid = $params->get('field_name'); //change the field name to whatever you have 

$app = JFactory::getApplication();
$menu = $app->getMenu();
$link = $menu->getItem($itemid)->link;

echo JRoute::_($link);

This will echo a SEF URL. If you want a standard URL then simply use echo $link;

Hope this helps

5
  • Wow, so simple! I thought I would have to query the database. I'll try this out today and let you know the results. Thanks a lot :) May 14, 2014 at 7:26
  • I had a similar need. When I retrieve the menu item with $menu->getItem($itemid) it provides me the expected object -- but the ->link attribute is empty. I could route using the alias directly, but I'm not sure if that's good form. Dec 4, 2014 at 16:25
  • @lcdservices - What version of Joomla?
    – Lodder
    Dec 4, 2014 at 16:27
  • Nevermind -- I found the problem. we have some menu items that are of type "text separator", which consequently do not have a link value. I had to condition my code to handle the possibility of a menu item of this type being selected incorrectly. Dec 4, 2014 at 16:39
  • @RustyJoomla - Glad it helped :)
    – Lodder
    Sep 21, 2015 at 12:51
1

It can be done a little faster with less codding:

JRoute::_('index.php?Itemid='.$itemid);

This will return a relative url for the selected menu item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.