Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento: remove page layouts

Tags:

magento

In magento the following page layouts are defined by default: empty, one_column, two_columns_left, two_columns_right and three_columns.

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?

I found that I can remove it from the app/core/community/Mage/Page/etc/config.xml, but I would like to do this without change any core source, to be updateable.

like image 875
GreenRover Avatar asked Nov 22 '11 12:11

GreenRover


2 Answers

I stumbled across this question looking for something similar and want to share my implementation. Maybe it is helpful for someone out there.

The following 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.

I created a module (in fact the very first module I've ever built for magento) and 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, we 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 I've added the remove_layouts directive. Finally we write our 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;
    }

}

Works and tested with Magento 1.7.0.

like image 162
Philipp Avatar answered Sep 28 '22 16:09

Philipp


Because the root layouts are parsed from config XML, and due to the way in which config XML is merged together, your simplest option (as you've surmised) is to edit app/core/community/Mage/Page/etc/config.xml.

If you are really concerned with not editing core files - always a legitimate & fun endeavor - you could create a module that can handle remove_layout directives, which you could add in your module's config under the same xpath. The class you would be rewriting is Mage_Page_Model_Config - see the _appendPageLayouts() and getPageLayouts() methods.

like image 29
benmarks Avatar answered Sep 28 '22 18:09

benmarks