I want to modify the content that shows up on my Magento website's product view page and I want to create my own custom block for use on the page as well.
What I've Done
Create Layout Updates For Product View Handle
To update what was displayed on the product's view page, I put the following
layout updates within my local.xml file:
<catalog_product_view>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<remove name="product.tierprices" />
<remove name="product.info.upsell" />
<remove name="product.clone_prices" />
<remove name="product.description" />
<remove name="product.tag.list" />
<remove name="product.info.addto" />
<remove name="product.info.addtocart" />
<remove name="product.info.downloadcontent" />
<remove name="product.info.extrahint" />
<remove name="product.info.options.wrapper.bottom" />
<remove name="product.info.container1" />
<remove name="product.info.container2" />
<remove name="alert.urls" />
<remove name="catalog.compare.sidebar" />
<remove name="cart_sidebar" />
<remove name="right.permanent.callout" />
<remove name="paypal.partner.right.logo" />
<remove name="right.poll" />
<remove name="bundle.tierprices" />
<remove name="product.attributes" />
<!-- This should override the original product.info block -->
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<!-- Custom MyNamespace_Videos_Block_Player block -->
<block type="videos/player" name="videoPlayer" as="videoPlayer" template="video/player.phtml"/>
</block>
</reference>
</catalog_product_view>
At this point, I confirmed the remove nodes worked. Obviously, because I hadn't
created the videos/player block class at this point, nothing showed up.
Custom Module
I created a custom module by creating the following folder structure under app/code/local:
- MyNameSpace
- MyNameSpace/Videos
- MyNameSpace/Videos/Block
- MyNameSpace/Videos/etc
I created the config file for the module at MyNameSpace/Videos/etc/config.xml and placed the following xml nodes within:
<?xml version="1.0"?>
<config>
<modules>
<mynamespace_videos>
<version>0.0.1</version>
</mynamespace_videos>
</modules>
<global>
<blocks>
<videos>
<class>MyNamespace_Videos_Block</class>
</videos>
</blocks>
</global>
</config>
I then enabled the module and verified that it worked.
Custom Block Class
I've created a custom block within MyNameSpace/Videos/Block/Player.phtml with the following:
class MyNamespace_Videos_Block_Player extends Mage_Core_Block_Template {
public function _toHtml() {
echo "Block's _toHtml() method called!";
parent::_toHtml();
}
}
Custom Template File
I then created the custom template file at design/frontend/mythemepackage/default/template/video/player.phtml that contains the following:
<!--Check to see if Magento sees this!-->
<?php Mage::log(get_class($this)); ?>
Modified catalog/product/view.phtml
To have my videos/player block show up within catalog/product/view.phtml, I
copied app/design/frontend/base/default/template/catalog/product/view.phtml to
my design package at
app/design/frontend/mythemepackage/default/template/catalog/product/view.phtml.
I then added the following line to the newly copied view template:
<b>This text will show up!</b>
<i>The following wont: </i>
<?php echo $this->getChildHtml('videoPlayer'); ?>
Outcome
With the above configuration, I cannot get the videoPlayer block to render.
Here's what I know:
- I can see the normal html text in my
catalog/product/view.phtmlfile. I cannot see what is echoed from$this->getChildHtml('videoPlayer'); - The
_toHtml()method ofMyNamespace_Videos_Block_Playerclass is not being called and I cannot see any output from myvideo/player.phtmltemplate. - If I copy
catalog.xmlfromapp/design/frontend/base/default/layout/catalog.xmltoapp/design/frontend/mythemepackage/default/layout/catalog.xmland then add myvideos/playerblock to it, I can see "Block's _toHtml() method called!" that has been echoed by thevideos/playerblock. The following is what the addition tocatalog.xmllooks like:
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="videos/player" name="videoPlayer" as="videoPlayer" template="video/player.phtml"/>
</block>
- Even though using a local version of
catalog.xmlcalls my block's_toHtml()method, nothing is output from my template.
Questions
I've spent quite a while doing troubleshooting for this. I've deleted the cache completely at var/cache and verified that the module is active and is working.
- What am I missing? What needs to happen to get the block to render?
- Why does copying
catalog.xmland adding my block there work but updating throughlocal.xmldoesn't?
<remove />uses block names in layout, it is not necessary/appropriate to nest<remove />directives in anything other layout update handles. If you want to break parent-child relationships (remove a block from a parent but leave it available for use) then you should use<acton method="unsetChild"><block>alias</block></action>in the reference. - benmarks