magento:删除页面布局

Gre*_*ver 11 magento

在Magento以下页面布局默认情况下,定义为:empty,one_column,two_columns_left,two_columns_rightthree_columns.

我想删除two_columns_left,two_columns_right我的布局,因为用户可以在CMS和产品设计部分选择了它,但它不工作.

如何更改XML配置文件以实现此目的?

我发现我可以从中删除它app/core/community/Mage/Page/etc/config.xml,但我想这样做而不需要更改任何核心源,可以更新.

小智 8

我偶然发现了这个问题,寻找类似的东西,并希望分享我的实现.也许对那里的人有帮助.

以下内容将从可用模板列表中删除空的2_columns_right和3_columns布局.只需更改下面的remove_layouts指令config.xml即可删除要删除的内容.

我创建了一个模块(实际上是我为magento构建的第一个模块)并将以下内容放在文件中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>
Run Code Online (Sandbox Code Playgroud)

接下来,我们需要config.xml找到一个/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>
Run Code Online (Sandbox Code Playgroud)

请注意,我已经添加了remove_layouts指令.最后我们编写自己的Labor_Templates_Model_Config类:

<?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;
    }

}
Run Code Online (Sandbox Code Playgroud)

使用Magento 1.7.0进行工作和测试.


ben*_*rks 3

因为根布局是从配置 XML 解析的,并且由于配置 XML 合并在一起的方式,所以最简单的选择(正如您所猜测的)是编辑app/core/community/Mage/Page/etc/config.xml.

如果您真的担心不编辑核心文件(始终是合法且有趣的努力),您可以创建一个可以处理remove_layout指令的模块,您可以将其添加到同一xpath下的模块配置中。您要重写的类是Mage_Page_Model_Config- 请参阅_appendPageLayouts()getPageLayouts()方法。