I was in the middle of writing some code for a Magento website, and I feel like I'm at odds with what I am trying to accomplish.
I am trying to write an extension which inserts 2 blocks:
Hello_Catalog_Block_Category_View: which overrides the Mage_Catalog_Block_Category_View Block with some extra functionality.
Hello_Catalog_Block_Custom: which is a customised class I want to create for this extension
Here's what I have tried to write in the config.xml file:
<blocks>
<catalog>
<rewrite>
<category_view>Hello_Catalog_Block_Category_View</category_view>
</rewrite>
<class>
<custom>Hello_Catalog_Block_Custom</custom>
</class>
</catalog>
</blocks>
Obvously if I tried this code when I refresh the browser, this doesn't work because I must have initialised the custom block the wrong way.
Now if I tried to write in this fashion:
<blocks>
<catalog>
<rewrite>
<category_view>Hello_Catalog_Block_Category_View</category_view>
</rewrite>
<class>Hello_Catalog_Block</class>
</catalog>
</blocks>
Now when I refresh the browser, the templates for Catalog Category view don't get rendered and I get the feeling it gets overridden by <class>Hello_Catalog_Block</class>
.
My question is, is there a way to write an extension that allows these 2 blocks to be used or together or would it just be a case where either you write an extension that overrides blocks or you write an extension that creates new blocks only, not both?
Thanks.
I think there's a disconnect between what you think a "custom block" will do and what they actually do. There's no way to just add something to config.xml
and have the block show up on the page.
If you want to create a custom block for your module, the first step is to configure a new top level section under blocks
<blocks>
<hello_catalog>
<class>Hello_Catalog_Block</class>
</hello_catalog>
</blocks>
The <hello_catalog>
node is you block's group name. When you use the above configuration, you're telling Magento
Hey Magento, if you see a block in the
hello_catalog
group, it's class name should start withHello_Catalog_Block
.
With the above in place, you'll be able to do things in Magento's layout update XML files (the XML files in app/design
) like this
<block type="hello_catalog/custom" name="me_custom_block" />
The above XML is creating a block of type hello_catalog/custom
. That's a block in the hello_catalog
group, with its class name being custom
. Magento will translate this into the full class name Hello_Catalog_Block_Custom
(using the information from config.xml
for the base name, and then lead-word-casing custom
.