【问题标题】:magento: remove page layoutsmagento:删除页面布局
【发布时间】:2013-10-09 12:57:26
【问题描述】:

在 magento 中,默认定义以下页面布局:emptyone_columntwo_columns_lefttwo_columns_rightthree_columns

我想为我的布局删除two_columns_lefttwo_columns_right,因为用户可以在 CMS 和产品设计部分选择它,但它不起作用。

如何更改 XML 配置文件来完成此操作?

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

【问题讨论】:

    标签: magento


    【解决方案1】:

    我偶然发现了这个问题,正在寻找类似的东西并想分享我的实现。也许它对那里的人有帮助。

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

    我创建了一个模块(实际上是我为 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>
    

    接下来,我们需要在/app/code/local/Labor/Templates/etc 中找到的config.xml

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

    注意,我添加了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;
        }
    
    }
    

    使用 Magento 1.7.0 工作和测试。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 2014-08-23
        • 1970-01-01
        • 2014-09-05
        • 1970-01-01
        相关资源
        最近更新 更多