overriding a magento block in multiple modules (and how to ignore the other ones)

前端 未结 4 823
梦如初夏
梦如初夏 2021-01-02 15:42

In Magento you can override a block (or helper, or model, etc.) from one module in another one (most common from Magento Adminhtml module). Now I have the problem that I wan

4条回答
  •  灰色年华
    2021-01-02 16:10

    I took a look in the class loading, and it doesn't look like there is a clean solution. The relevant classname loading code is this:

        if (isset($this->_classNameCache[$groupRootNode][$group][$class])) {
            return $this->_classNameCache[$groupRootNode][$group][$class];
        }
    
        ...
    
        if (isset($config->rewrite->$class)) {
            $className = (string)$config->rewrite->$class;
    

    So it looks like there is no such thing as a rewritten-rewrite. The one that gets put into the configuration array is the one that gets loaded, and once cached it simply gets returned as requested. I don't see an easy way to hack the cache, either. You could try to get precedence for your module (by renaming it alphabetically, I assume), but that's a total hack and likely to fail mysteriously later on. That kills the config possibilities.

    Also obnoxious: it looks like Magento only requests that grid in two places, within Mage_Adminhtml_Block_Catalog_Product (which you can easily override), but also in Mage_Adminhtml_Catalog_ProductController (d'oh!). Depending on your use case, consider overriding the catalog product block and replacing the _prepareLayout() function with your own. If you want to override the controller as well, you'll want to create your own controller (which descends from Mage_Adminhtml_Catalog_ProductController) and define a rewrite within your configuration to direct admin/catalog/product/grid to your action. A pain, but at least accomplishes your goal.

    Hope that helps,

    Joe

提交回复
热议问题