在magento中,默认情况下定义了以下页面布局: empty
, one_column
, two_columns_left
, two_columns_right
和 three_columns
。
我想删除 two_columns_left
, two_columns_right
对于我的布局,因为用户可以在CMS和产品设计部分中选择它,但它不起作用。
如何更改XML配置文件以实现此目的?
我发现我可以从中删除它 app/core/community/Mage/Page/etc/config.xml
,但我想在不更改任何核心源的情况下执行此操作,以便可更新。
因为根布局是从配置XML解析的,并且由于配置XML合并在一起的方式,您最简单的选项(如您所推测的)是编辑 app/core/community/Mage/Page/etc/config.xml
。
如果你是 真 关注不编辑核心文件 - 总是合法和有趣的尝试 - 您可以创建一个可以处理remove_layout指令的模块,您可以在同一个xpath下添加模块的配置。你要重写的课程是 Mage_Page_Model_Config
- 看到了 _appendPageLayouts()
和 getPageLayouts()
方法。
我偶然发现了这个问题,寻找类似的东西并希望分享我的实现。也许对那里的人有帮助。
以下内容将从可用模板列表中删除空的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>
接下来,我们需要一个 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>
请注意,我已经添加了 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进行工作和测试。
因为根布局是从配置XML解析的,并且由于配置XML合并在一起的方式,您最简单的选项(如您所推测的)是编辑 app/core/community/Mage/Page/etc/config.xml
。
如果你是 真 关注不编辑核心文件 - 总是合法和有趣的尝试 - 您可以创建一个可以处理remove_layout指令的模块,您可以在同一个xpath下添加模块的配置。你要重写的课程是 Mage_Page_Model_Config
- 看到了 _appendPageLayouts()
和 getPageLayouts()
方法。
我偶然发现了这个问题,寻找类似的东西并希望分享我的实现。也许对那里的人有帮助。
以下内容将从可用模板列表中删除空的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>
接下来,我们需要一个 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>
请注意,我已经添加了 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进行工作和测试。