Monday, July 16th 2012


Magento: Extend/Rewrite Block
posted @ 7:11 am in [ Magento -Technology -Web Design ]

To extend/rewrite an existing Block, you need to create your new extension, and your extended block and then rewrite the current block using your config.xml file.

1. Create your new extension

1.1 Folder for your blocks

/app/code/local/Zuneta/Review/Block

1.2 Folder for your configuration file

/app/code/local/Zuneta/Review/etc

2. Extend the block you want to extend/rewrite

2.1 Extended block

/app/code/local/Zuneta/Review/Block/Product/View/List.php

/**
* Detailed Product Reviews
*
* @category Zuneta
* @package Zuneta_Review
* @author Alex Judd
*/
class Zuneta_Review_Block_Product_View_List extends Mage_Review_Block_Product_View_List
{
public function getReviewUrlSection($id, $section)
{
return Mage::getUrl($section . '/*/view', array('id' => $id));
}
}

Notes:

Here we have extended the core block, namely /app/code/core/Mage/Review/Block/Product/View/List.php with our own local version to add a new function allowing us to specify which section we would like to refer to when you view a review.

3. Extend/Rewrite the current block using our XML config.xml file

3.1 /app/code/local/Zuneta/Rewrite/etc/config.xml






0.1.0






Zuneta_Review_Block_Product_View_List




Notes:

The trick here is to specify within the ‘global’ section that we want to talk to the ‘blocks’ section.

Then when in the ‘blocks’ section we want the ‘review’ block to be the one begin rewritten, and as our code is 3 levels deep, we want to rewrite the ‘product_view_list’ block as that relates to /app/code/core/Mage/Review/Block/Product/View/List.php

Then we simply specify the whole structure of our block in the rewrite contents

4. Enabling our block

4.1 /app/etc/modules/Zuneta_Review.xml

We need a /app/etc/modules/Zuneta_Review.xml file in order to enable our module





true
local



5. Done!

We should now be good to go and our new function can be called, for example in my case I am modifying the product template

/app/design/frontend/default/zuneta/template/review/product/view/list.phtml

to specify that I want to use the ‘review’ rewrite for all my detailed review links

htmlEscape($_review->getTitle()) ?> __('Review by %s', $this->htmlEscape($_review->getNickname())) ?>


Leave a Reply