5

我正在开发一个“为了好玩”的项目,我希望将它作为一个“为了好玩”的开源项目发布。我已经构建了一个 Arduino 联网订单通知器,并且正在构建它的 Magento adminhtml 界面。对于我正在做的事情来说,这似乎过于复杂,但这还有其他对我有用的应用程序,我正在尝试学习新的东西。

话虽如此,我在 Magento 后端的“报告”旁边创建了一个新的“Arduino”adminhtml 菜单项。在某些时候,来自 Arduino 的特定反馈可能会自动禁用模块,在这种情况下,我的目标是从页面中删除菜单项,除非有其他子菜单项,在这种情况下,我只删除模块特定的菜单项. 同样,这对于这个项目来说似乎不太现实,但为了尽可能“正确”地做到这一点,这是我的目标,让其他人可能会添加“Arduino”菜单项的可能性保持开放。

现在,如果缓存已关闭,我可以正常工作,没有问题。这是另一种说法,“这不起作用,因为显然每个人都会打开缓存。” 也就是说,这是我开始的代码:

/**
 * 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;

}

这显然增加了一些开销,但它只在 adminhtml 部分。现在,我的目标是有一个“好的”解决方案,但我不确定是否有一个。我已经阅读了这篇非常好的博客文章,虽然我认为这样的事情可能对我的目的有用(相反),但我更愿意创建一个不使用类重写的模块,这样我就不需要担心了与其他模块重写同一类。

有人对我如何解决这个问题有任何建议吗?如果我要将自定义菜单项添加到现有的“父”菜单项(例如,如果我要在“系统”下方添加我的 Arduino 选项),是否存在相同的缓存问题?

感谢您的时间和这个伟大的资源!

编辑:

  • 登录/退出似乎可以“修复”我现有的问题并正确更新菜单。我当然不想强迫用户注销,但也许某处可能有解决方案?
4

2 回答 2

1

似乎使它工作的唯一方法是每次进行更改时清理菜单缓存。您可以使用下一个代码来做到这一点:

Mage::app()->getCache()->clean(
    Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(Mage_Adminhtml_Block_Page_Menu::CACHE_TAGS)
);
于 2012-10-18T20:17:34.777 回答
0

关于如何删除项目而不在菜单上留下空文本的代码示例:

/**
 * 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;

}
于 2014-04-22T14:17:28.930 回答