0

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?

1 Answer 1

1

The code you commented out is here to add some meta datas in the head block (meta title, keywords, ...etc). So, the site still works without it but there will be SEO issue.

If you want to understand the general purpose of the _prepareLayout method (not only the one for the Mage_Catalog_Block_Product_View class, you will have to read the core sources : when a block is created, app/code/core/Mage/Core/Model/Layout.php::createBlock is called. In this method $block->setLayout($this); is called. You can find in app/code/core/Mage/Core/Block/Abstract.php the setLayout method that is finally calling the _prepareLayout method...

Each block that extends Mage_Core_Block_Abstract can have its own _prepareLayout method in order to modify the layout for different purpose. See for example Mage_Cms_Block_Page, Mage_Sales_Block_Order_View,etc, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.