Last active
April 1, 2019 11:06
-
-
Save herveguetin/7c78e3991687744f164b to your computer and use it in GitHub Desktop.
Dynamically add items to admin menu in Magento
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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