Get any property of the active menu item class in Joomla! 3

When a menu item is clicked, Joomla! 3 loads a lot of information for the programmers in a standard class object variable about the current menu item. Programmers can query the class object by key to find its corresponding value, like, id, menutype, title, alias, note, route, link, type, language, component_id, component, query, _errors, etc. from it.

As it's already loaded in memory when Joomla! runs, querying the menu item does not overload your server.

Let us do some coding. First, we will get a hold on to Joomla! Application object, get the active menu item and get any property from it.

// Joomla! Site Application Object
$app = JFactory::getApplication();
// Menu class of type JMenuSite
$menus = $app->getMenu();
// Get Active Menu Item which is a type of stdClass Object
$activeMenu = $menus->getActive();

We now have gotten the Class object of active menu item. Let us examine it using var_dump command.

Here is var_dump output:

object(JMenuItem)#179 (22)
{
     ["id"]=> string(3) "108"
     ["menutype"]=> string(8) "mainmenu"
     ["title"]=> string(5) "About"
     ["alias"]=> string(5) "about"
     ["note"]=> string(0) ""
     ["route"]=> string(5) "about"
     ["link"]=> string(46) "index.php?option=com_content&view=article&id=1"
     ["type"]=> string(9) "component"
     ["level"]=> string(1) "1"
     ["language"]=> string(1) "*"
     ["browserNav"]=> string(1) "0"
     ["access"]=> string(1) "1"
     ["params":protected]=> object(Joomla\Registry\Registry)#253 (3)
     {
          ["data":protected]=> object(stdClass)#255 (33)
          {
               ["show_title"]=> string(0) ""
               ["link_titles"]=> string(0) ""
               ["show_intro"]=> string(0) ""
               ["info_block_position"]=> string(1) "0"
               ["show_category"]=> string(1) "0"
               ["link_category"]=> string(1) "0"
               ["show_parent_category"]=> string(0) ""
               ["link_parent_category"]=> string(0) ""
               ["show_author"]=> string(1) "0"
               ["link_author"]=> string(0) ""
               ["show_create_date"]=> string(1) "0"
               ["show_modify_date"]=> string(0) ""
               ["show_publish_date"]=> string(1) "0"
               ["show_item_navigation"]=> string(0) ""
               ["show_vote"]=> string(0) ""
               ["show_icons"]=> string(0) ""
               ["show_print_icon"]=> string(0) ""
               ["show_email_icon"]=> string(0) ""
               ["show_hits"]=> string(1) "0"
               ["show_noauth"]=> string(0) ""
               ["urls_position"]=> string(0) ""
               ["menu-anchor_title"]=> string(0) ""
               ["menu-anchor_css"]=> string(0) ""
               ["menu_image"]=> string(0) ""
               ["menu_text"]=> int(1)
               ["page_title"]=> string(0) ""
               ["show_page_heading"]=> int(0)
               ["page_heading"]=> string(0) ""
               ["pageclass_sfx"]=> string(0) ""
               ["menu-meta_description"]=> string(0) ""
               ["menu-meta_keywords"]=> string(0) ""
               ["robots"]=> string(0) ""
               ["secure"]=> int(0)
          }
          ["initialized":protected]=> bool(true) ["separator"]=> string(1) "."
} ["home"]=> string(1) "0" ["img"]=> string(0) "" ["template_style_id"]=> string(1) "0" ["component_id"]=> string(2) "22" ["parent_id"]=> string(1) "1" ["component"]=> string(11) "com_content" ["tree"]=> array(1) { [0]=> string(3) "108" } ["query"]=> array(3) { ["option"]=> string(11) "com_content" ["view"]=> string(7) "article" ["id"]=> string(1) "1" } ["_errors":protected]=> array(0) { } }

To get value of any of the properties, you can call it by the "key" of your variable as below:

echo $active->alias;

Below is a one-liner or shortcut:

echo JFactory::getApplication()->getMenu()->getActive()->link;

or

echo JFactory::getApplication()->getMenu()->getActive()->alias;

Other articles of this category: