2

Not very experienced with Magento layouts, so forgive me, if the question is too vague.

I have defined a layout in config.xml similar to this:

 <page>
    <layouts>
        <campaign module="page" translate="label">
            <label>Campaign</label>
            <template>page/campaign.phtml</template>
            <layout_handle>campaign</layout_handle>
        </campaign>
    </layouts>
</page>

But I seem not the get the <layout_handle> definition correct, have tried define a separate file, using another handle, etc. Nothing seems to work, the default layout for the page (category) is always used.

Edit: the code is in module's config.xml. I wish to use my own layout instead of the catalog_category_default

6
  • Where did you put this code? Did you cleared the cache?
    – SeStro
    Dec 3, 2015 at 17:51
  • Config.xml. Cache not in use. Dec 3, 2015 at 17:53
  • Do you want add new layout for CMS pages or change layout of category page?
    – SeStro
    Dec 3, 2015 at 18:02
  • Change layout of category page. The layout should be selectable from the custom design / page layout drop down in admin. Dec 3, 2015 at 18:06
  • Ok, I get it. Check config scope ('Choose Store View' select list). Maybe your changes for global scope are overriden by store scope?
    – SeStro
    Dec 3, 2015 at 18:38

1 Answer 1

1

Layout handle, defined in Page Layout, for some reason, works only for CMS pages (Alan Storm described it here). If you want add custom handle to specified categories, you could use this code:

app/etc/modules/SeStro_CatalogPageLayoutHandle.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <SeStro_CatalogPageLayoutHandle>
            <active>true</active>
            <codePool>local</codePool>
        </SeStro_CatalogPageLayoutHandle>
    </modules>
</config>

app/code/local/SeStro/CatalogPageLayoutHandle/etc/config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <SeStro_CatalogPageLayoutHandle>
            <version>0.1.0</version>
        </SeStro_CatalogPageLayoutHandle>
    </modules>

    <global>
        <resources>
            <sestro_catalogpagelayouthandle_setup>
                <setup>
                    <module>SeStro_CatalogPageLayoutHandle</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
            </sestro_catalogpagelayouthandle_setup>
        </resources>

        <events>
            <controller_action_layout_load_before>
                <observers>
                    <addCategoryLayoutHandle>
                        <class>SeStro_CatalogPageLayoutHandle_Model_Observer</class>
                        <method>addCategoryLayoutHandle</method>
                    </addCategoryLayoutHandle>
                </observers>
            </controller_action_layout_load_before>
        </events>

        <page>
            <layouts>
                <bazinga>
                    <label>Bazinga</label>
                    <template>page/1column.phtml</template>
                    <layout_handle>bazinga</layout_handle>
                </bazinga>
            </layouts>
        </page>
    </global>
</config>

app/code/local/SeStro/CatalogPageLayoutHandle/Model/Observer.php

<?php
class SeStro_CatalogPageLayoutHandle_Model_Observer
{

    public function addCategoryLayoutHandle(Varien_Event_Observer $observer)
    {
        $category = Mage::registry('current_category');

        if (!($category instanceof Mage_Catalog_Model_Category)) {
            return;
        }

        if ($category->getLayoutHandle()) {
            $layoutHandleName = str_replace(' ', '_', $category->getLayoutHandle());
            $update = $observer->getEvent()->getLayout()->getUpdate();
            $handles = $update->getHandles();
            $update->resetHandles();

            foreach ($handles as $handle) {
                $update->addHandle($handle);
                if (trim($handle) === 'catalog_category_layered') {;
                    $update->addHandle($layoutHandleName);
                }
            }
        }
    }
}

app/code/local/SeStro/CatalogPageLayoutHandle/sql/catalogpagelayouthandle_setup/install-0.1.0.php

<?php

$installer = $this;
$installer->startSetup();

$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'layout_handle', array(
    'group'             => 'Custom Design',
    'input'             => 'text',
    'type'              => 'text',
    'frontend_class'    => 'validate-alphanum-with-spaces',
    'label'             => 'Layout Handle',
    'backend'           => '',
    'visible'           => true,
    'required'          => false,
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

$installer->endSetup();

local.xml

<?xml version="1.0" encoding="UTF-8" ?>
<layout>
    <bazinga>
        <reference name ="content">
            <block type="core/text" name="bazinga" before="-">
                <action method="setText"><text>bazinga!</text></action>
            </block>
        </reference>
    </bazinga>
</layout>

Then go to Catalog->Manage Categories->Category->Custom Design tab, and add 'bazinga' as Layout Handle. Of course you could use another handle name and content. You could also use your module to add observer and category attribute.

2
  • I did not try this, but I'm sure this would work. I have been successfully been able to change the template page.I would however like to use the standard 1column.pthml and just define my own layout, but I just can't get it to work. In your example, how do I define the bazinga XML layout? Dec 4, 2015 at 7:49
  • I eddited my answer. I hope that it solve your problem ;]
    – SeStro
    Dec 6, 2015 at 13:39

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.