I know that magento uses .phtml "template" files for the view. Each template has a block class where we can prepare some values or make some checks which we then can use in the view, e.g. check if the users is logged in.
I discovered a function named _prepareLayout()
:
app\code\core\Mage\Catalog\Block\Product\View.php
class Mage_Catalog_Block_Product_View extends Mage_Catalog_Block_Product_Abstract
{
protected function _prepareLayout()
{
$this->getLayout()->createBlock('catalog/breadcrumbs');
$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
$product = $this->getProduct();
$title = $product->getMetaTitle();
if ($title) {
$headBlock->setTitle($title);
}
$keyword = $product->getMetaKeyword();
$currentCategory = Mage::registry('current_category');
if ($keyword) {
$headBlock->setKeywords($keyword);
} elseif ($currentCategory) {
$headBlock->setKeywords($product->getName());
}
$description = $product->getMetaDescription();
if ($description) {
$headBlock->setDescription( ($description) );
} else {
$headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
}
if ($this->helper('catalog/product')->canUseCanonicalTag()) {
$params = array('_ignore_category' => true);
$headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
}
}
return parent::_prepareLayout();
}
What exactly is happening here?
I found that this is the corresponding layout file:
app\design\frontend\venedor\company\layout\catalog.xml
<!--
Product view
-->
<catalog_product_view translate="label">
<label>Catalog Product View (Any)</label>
<!-- Mage_Catalog -->
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
<reference name="head">
<action method="addJs"><script>varien/product.js</script></action>
<action method="addJs"><script>varien/product_options.js</script></action>
<action method="addJs"><script>varien/configurable.js</script></action>
<action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
</reference>
...
If I comment the function out, then the site still works as expected, so why is it needed?
class Mage_Catalog_Block_Product_View extends Mage_Catalog_Block_Product_Abstract
{
protected function _prepareLayout()
{
// $this->getLayout()->createBlock('catalog/breadcrumbs');
// $headBlock = $this->getLayout()->getBlock('head');
// if ($headBlock) {
// $product = $this->getProduct();
// $title = $product->getMetaTitle();
// if ($title) {
// $headBlock->setTitle($title);
// }
// $keyword = $product->getMetaKeyword();
// $currentCategory = Mage::registry('current_category');
// if ($keyword) {
// $headBlock->setKeywords($keyword);
// } elseif ($currentCategory) {
// $headBlock->setKeywords($product->getName());
// }
// $description = $product->getMetaDescription();
// if ($description) {
// $headBlock->setDescription( ($description) );
// } else {
// $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
// }
// if ($this->helper('catalog/product')->canUseCanonicalTag()) {
// $params = array('_ignore_category' => true);
// $headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
// }
// }
return parent::_prepareLayout();
}
What is the purpose of prepareLayout?