Every block, or group of blocks is declared in the config.xml
file of a module like this (inside the <global>
tag).
Here is an example from the catalog module
<blocks><!-- marks definition of a block group -->
<catalog><!-- unique alias for blocks in the module -->
<class>Mage_Catalog_Block</class><!-- class prefix for all blocks -->
</catalog>
</blocks>
This means that a block can be instantiated using the alias catalog/class_name_here
where class_name_here
is the rest of the class path starting from the prefix.
This means catalog/class_name_here
will be mapped by default to Mage_Catalog_Block_Class_Name_Here
.
To rewrite a block you need to create a module that depends on the module you are trying to change (Magento_Catalog
) in my example.
And you need to add this in the config.xml
under the <global>
tag.
<blocks>
<catalog><!-- alias of the block group you are rewriting -->
<rewrite><!-- reserved tag: specify that you are rewriting something -->
<class_name_here>YourNamespace_YourModule_Block_Your_New_Class_Here</class_name_here> <!-- tag: the rest of the alias of the class you are rewriting. value: the name of your class that rewrites the core class -->
</rewrite>
</catalog>
</blocks>
Then create the class YourNamespace_YourModule_Block_Your_New_Class_Here
(following the ZF folder structure) and make this class extend the original class.
class YourNamespace_YourModule_Block_Your_New_Class_Here extends Mage_Catalog_Block_Class_Name_Here
{
//your awesome code here
}
When you are done, disable compilation and enable it again (if needed) and clear the cache.
This will not work for abstract blocks.
It only works for classes that get instantiated.
Example
Let's assume that you want to rewrite the file app\code\core\Mage\Catalog\Block\Product\View\Options\Type\Select.php which has the class Mage_Catalog_Block_Product_View_Options_Type_Select
in your own module Marius_Test.
Then you would need this entry in your config.xml
:
<blocks>
<catalog>
<rewrite>
<product_view_options_type_select>Marius_Test_Block_Catalog_Block_Product_View_Options_Type_Select</product_view_options_type_select>
</rewrite>
</catalog>
</blocks>
app\code\local\Marius\Test\Block\Catalog\Product\View\Options\Type\Select.php:
class Marius_Test_Block_Catalog_Product_View_Options_Type_Select extends Mage_Catalog_Block_Product_View_Options_Type_Select
{
//your awesome code here
}