How to Get Menu in Joomla
In Joomla a Menu is the set of navigation links you can have as a main menu (at the top of site) or side menu or footer menu. Each of the individual navigation link is a Menu item. You can easily get menu items in Joomla.
To get the menu:
use Joomla\CMS\Factory;
$app = Factory::getApplication();
$menu = $app->getMenu();
The $menu will have entries for all the menu items. To get at the individual menu items, you can apply a filter to the $menu using getItems() method to select the particular menu items you are interested in.
To get menu items by attribute:
$items = $menu->getItems('menutype', 'mainmenu');
To filter the specific menu items you want, you pass an array of properties and an array of corresponding values which those properties should have. Unpublished menu items are not returned.
Properties
The variable $items is an array of menu item objects. It contains the following:
- id
- menutype
- title
- alias
- note
- route
- link
- type
- level
- language
- browserNav
- access
- home
- template_style_id
- component_id
- parent_id
- component
- tree (Array)
- query (Array)
All the other attributes of the menu item are stored in the params field in the database and accessed using the getParams() method.
$params = $menuitem->getParams();
$displayed = $params->get("menu_show");
$show_tags = $params->get("show_tags");
Individual Menu Items
There are some methods available to get directly to certain menu items.
To get the active menu, and then its parameters:
$current = $menu->getActive();
$params = $current->params;
$yourParameter = $params->get('yourParameter');
To get the default item by language code:
$default = $menu->getDefault();
To get the menu item associated with a specific id $itemid:
$menuitem = $menu->getItem($itemid);