5

I am working on a "for fun" project that I hope to release as a "for fun" open source project. I've built an Arduino web-connected order notifier and I am building up the Magento adminhtml interface side of it. This might seem overly complicated for what I am doing, but this has other applications that are useful to me and I'm trying to learn something new.

With that said, I have create a new "Arduino" adminhtml menu item next to "Reports" in the Magento backend. There are certain times when particular feedback from the Arduino may automatically disable the module, in which case my goal is to remove the menu item from the page UNLESS there are other sub-menu items, in which case I only remove my module specific menu items. Again, this may not seem very realistic for this project but in an effort to do this as "right" as possible that is my goal, to leave the possibility open that somebody else could potentially add a "Arduino" menu item.

Now, I have this working GREAT, without issue, IF caching is turned off. Which is another way of saying, "this doesn't work because obviously everybody will have cache turned on." That said, here is the code I started with:

/**
 * Used to disable the Arduino menu option dynamically.
 *
 * @param Varient_Event_Observer $observer
 */
public function controllerActionLayoutGenerateBlocksBefore(Varien_Event_Observer $observer)
{
    //We only want to disable the arduino menu if our module was specifically disabled in backend
    if(Mage::helper('mynamespace_myarduinomodule')->isDisabled())
    {
        /* @var $arduinoItems Varien_Simplexml_Element */
        /* @var $parent Varien_Simplexml_Element */
        $arduinoItems = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu/arduinomenu/children');
        $parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');

        //If there are other menu options in the arduino menu option we only want to hide
        //the menu items related to our specific module, leaving the top level menu item alone
        if($arduinoItems->children()->count() > 1)
        {
            $parent->setNode("arduino/children/myarduinomodule", "", true);
        }
        //But, if not, then we want to hide the entire arduino tab
        else
        {
            $parent->setNode("arduino", "", true);
        }
    }

    return $this;

}

This obviously adds some overhead, but it's only in the adminhtml section. Now, my goal is to have a "good" solution, but I'm not sure if there is one. I've read this very good blog post and while I think something like this might work for my purposes (in reverse), I would prefer to create a module that doesn't utilize class rewrites so that I don't need to be concerned with other modules rewriting the same class.

Does anybody have any advice on how I might approach this problem? Is the same caching an issue if I were to add my custom menu items to an existing "parent" menu item (for instance if I were to add my Arduino options below "System")?

Thank you for your time and this great resource!

EDIT:

  • logging in/back out seems to "fix" my existing problem and update the menu properly. I don't want to force a user to logout of course, but perhaps there may be a solution there somewhere?
6
  • I'm starting to think that my solution may be acceptable for my particular case, I can simply redirect a user back to the dashboard or display an appropriate message if they try to access one of my custom backend pages/controller. Still I'd love to automatically remove my menu option before the user logs out of the backend. Commented Mar 18, 2012 at 1:37
  • If I am not mistaken, you would do something like that: Lets say, you have many extensions which you want to collect under a menu that available depend on the extensions. If customer bought an extension and installed the system, the extension options should appear in the menu. Right? Also, what is your problem exactly? Commented Mar 18, 2012 at 11:38
  • If you need to hide things, I would use some jQuery or Prototype to dynamically hide the elements. Have the page look up a field in the database (or somewhere) and go from there Commented Mar 25, 2012 at 0:35
  • You need to punch a hole in your cache, heres a good read: stackoverflow.com/questions/8126548/… Commented Apr 4, 2012 at 2:22
  • Keep in mind that most of the plugins you install, ask you to log out and in again, so all of their changes are reflected, so I guess it would be ok to ask the user to do that as well.
    – Alex Siri
    Commented Apr 13, 2012 at 11:19

2 Answers 2

1

It seems that the only way to make it work is to clean menu cache everytime you make changes. You can do it using next code:

Mage::app()->getCache()->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(Mage_Adminhtml_Block_Page_Menu::CACHE_TAGS)
);
0

Code sample on how to remove the item w/o leaving the empty text on the menu:

/**
 * Used to disable the Arduino menu option dynamically.
 *
 * @param Varient_Event_Observer $observer
 */
public function controller_action_layout_render_before(Varien_Event_Observer $observer)
{
    //We only want to disable the arduino menu if our module was specifically disabled in backend
    if(Mage::helper('mynamespace_myarduinomodule')->isDisabled())
    {
    $_config = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode();


    if($_config->menu->arduino->children){
        unset($_config->menu->arduino->children->myarduinomodule);
    }else{
        // hide the tab altogether
        unset($_config->menu->arduino);
    }
    Mage::getSingleton('admin/config')->getAdminhtmlConfig()->setXml($_config);

    return $this;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.