Skip to content

Instantly share code, notes, and snippets.

@herveguetin
Last active April 1, 2019 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herveguetin/7c78e3991687744f164b to your computer and use it in GitHub Desktop.
Save herveguetin/7c78e3991687744f164b to your computer and use it in GitHub Desktop.
Dynamically add items to admin menu in Magento
<?xml version="1.0"?>
<config>
...
<adminhtml>
<events>
<controller_action_layout_render_before>
<observers>
<module_update_admin_menu>
<class>My_Module_Model_Observer</class>
<method>updateAdminMenu</method>
</module_update_admin_menu>
</observers>
</controller_action_layout_render_before>
</events>
<menu>
<MAIN_MENU_ITEM>
<children>
<MENU_ITEMS_CONTAINER translate="title" module="module">
<title>Menu Items Container title</title>
<sort_order>1000</sort_order>
<children>
<!-- Children are added dynamically -->
</children>
</MENU_ITEMS_CONTAINER>
</children>
</MAIN_MENU_ITEM>
</menu>
</adminhtml>
...
</config>
<?php
class My_Module_Model_Observer {
/**
* Update admin menu with dynamic items
*/
public function updateAdminMenu()
{
$menu = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu/MAIN_MENU_ITEM/children/MENU_ITEMS_CONTAINER/children');
// Repeat numbered steps below as many times as you want to add items to the admin menu
// 1. Create $xml which is a valid admin menu item definition
$xml = '<dynamic_item><title>Dynamic Item</title><sort_order>10</sort_order><action>adminhtml/some/route</action></dynamic_item>';
// 2. Make a config node with $xml content
$node = new Mage_Core_Model_Config_Element($xml);
// 3. Append $node to existing loaded menu node
$menu->appendChild($node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment