jtanmay
jtanmay

Reputation: 2647

how to get joomla menu object based on name?

This question is bit specific to joomla.

I know if with the code $menu = &JSite::getMenu() I can get the reference object of the complete menu. But how can i get a specif menu based on the name?

My Scenario : I have footer-menu with items : home | about us | rules | privacy policy.

I need to display links to two menu items Rules and privacy policy in a component. I cannot hard code the links, as the itemid would be different in development and production environment.

Do we have some workaround like $menu = &JSite::getMenu()->get('footer-menu')->getMenuItem('rules'); which can give me refrence object to a particular menu item, from which I can create my links for that particular article.

Thanks, Tanmay

Upvotes: 0

Views: 1845

Answers (2)

iimos
iimos

Reputation: 5037

Method #1:

$menu = & JSite::getMenu();
$item = $menu->getItems('link', 'index.php?option=com_content&view=article&id=1', true);

Method #2:

$menu = & JSite::getMenu();
$item = $menu->getItems('alias', 'rules', true);

Upvotes: 2

Garrett Bluma
Garrett Bluma

Reputation: 1312

As far as I know, there isn't a built-in way to do this. But I feel your pain.

Here's an adaptation of a function I built before. It's not recursive, so you'll only get one level deep in a menu hierarchy, but that was enough for me.

function getMenuItems( $parentAlias ) {
    $db =& JFactory::getDBO();
    $sql = 'SELECT * FROM #__menu WHERE parent in ' .
           '(SELECT id FROM #__menu WHERE alias='.$db->Quote($parentAlias).') '.
           'AND published=1 ORDER BY ordering';
    $db->setQuery($sql);
    $results = $db->loadObjectList();
}

Let me know if this works for you.

Upvotes: 2

Related Questions