I need to rewrite a Magento core block file, which full path is app/code/core/Mage/Core/Block/Template.php, in order to rewrite the getTemplateFile() function. I have no problem with rewriting them, but the results are strange. In the getTemplateFile() function I have $this->getTemplate(), which returns a template. When I print_r($this->getTemplate()) in the core block function, it gives me all the PHTMLs that are being loaded, while in the rewritten block I get only some of them. Do you have any ideas why is that happening? I am using Magento CE 1.8.1.0.

有帮助吗?

解决方案

You can't rewrite the parent class of a block/model/helper in Magento, you can only rewrite a specific instance.

Here's how Magento's class rewriting works. Whenever core Magento code (and well coded modules) instantiate an object, they do it with a factory method.

$layout->createBlock('core/template');

The core/template string is a class alias. Magento uses this to lookup the actual class name to use. By default, this is Mage_Core_Block_Template. When you create a rewrite, you're telling Magento

Hey, whenever you create a core/template block object? Use my class instead.

The problem? There are many blocks in Magento which inherit from the core/template. When Magento instantiates one of these

$layout->createBlock('page/html_head')

It resolves to the class Mage_Page_Block_Html_Head, which inherits from the core template class

#File: app/code/core/Mage/Page/Block/Html/Head.php
class Mage_Page_Block_Html_Head extends Mage_Core_Block_Template
{
}

In other words, there's no way for the rewrite system to know about your swapped in core/template class.

So when you say

in the core block function, it gives me all the PHTMLs that are being loaded, while in the rewritten block I get only some of them

It sounds like you're seeing PHTMLs for the core/template blocks, but any blocks that inherit from Mage_Core_Block_Template are skipped.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top