How to remove page layouts in Magento

I would like to remove two_columns_left, two_columns_right for my layout, since the user can choose it in CMS and product design section, but it doesn’t work.

How do I change an XML configuration file to accomplish this?

Chris Stewart Asked on November 26, 2015 in Magento.
Add Comment
1 Answer(s)
Best answer

The following will create a module that will remove the empty, 2_columns_right and 3_columns layouts from the list of available templates. Just change the remove_layouts directive in the config.xml below to remove whatever you want to remove.

You would need to placed the following in the file app/etc/modules/Labor_Templates.xml:

<?xml version="1.0"?>
<!--
/**
 * This module changes the available templates. Only "1 column" and
 * "2 column-left" will be available.
 */
-->
<config>
    <modules>
        <Labor_Templates>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Page />
            </depends>
        </Labor_Templates>
    </modules>
</config>

Next, you need a config.xml found in /app/code/local/Labor/Templates/etc:

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Overrides the config to only allow "1 column" and "2 column left" layouts.
 */
-->
<config>
    <modules>
        <Labor_Templates>
            <version>1.0.0</version>
        </Labor_Templates>
    </modules>
    <global>
        <models>
            <template>
                <class>Labor_Templates_Model</class>
            </template>
            <page>
                <rewrite>
                    <config>Labor_Templates_Model_Config</config>
                </rewrite>
            </page>
        </models>
        <page>
            <remove_layouts>
                <layouts>empty,two_columns_right,three_columns</layouts>
            </remove_layouts>
        </page>   
    </global>
</config>

Notice, that you need to add the remove_layouts directive.

Finally you need to  write your own Labor_Templates_Model_Config class:

<?php
/**
 * Overrides the Overrides the core module Mage_Page_Model_Config in order to
 * remove unused template layouts. This is done by handling remove_layout
 * directives.
 */
class Labor_Templates_Model_Config extends Mage_Page_Model_Config {

    const XML_PATH_PAGE_REMOVE_LAYOUTS = 'global/page/remove_layouts';

    /**
     * Initialize page layouts list
     *
     * @return Labor_Templates_Model_Config
     */
    protected function _initPageLayouts()
    {
        parent::_initPageLayouts();
        return $this->_removePageLayouts(self::XML_PATH_PAGE_REMOVE_LAYOUTS);
    }

    /**
     * Removes page layouts found in the remove_layouts XML directive
     * 
     * @return Labor_Templates_Model_Config 
     */
    protected function _removePageLayouts($xmlPath)
    {
        if (!Mage::getConfig()->getNode($xmlPath) || !is_array($this->_pageLayouts)) {
            return $this;
        }
        foreach (explode(',', (string)Mage::getConfig()->getNode($xmlPath)->children()->layouts) as $toRemove) {
            unset($this->_pageLayouts[$toRemove]);
        }
        return $this;
    }

}
Chris Stewart Answered on November 26, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.