【问题标题】:Magento 2 Remove Layout OptionMagento 2 删除布局选项
【发布时间】:2017-12-04 22:24:26
【问题描述】:

我正在 Magento 2 中创建自定义主题。 在页面、类别、产品等的“设计”选项卡中选择布局时,我有以下选项:

  1. 1 列
  2. 2 列,左栏
  3. 2 列带右栏
  4. 3 列

我已经想出了如何将我自己的布局添加到这组选项中。

我的问题:如何删除(或在紧要关头隐藏)不需要的核心布局?例如,我的主题根本不需要 3 列布局,因此我不希望这是一个选项。

【问题讨论】:

    标签: magento2


    【解决方案1】:

    好的,这是你需要做的:

    创建一个新的Moduleapp/code/<Vendor>/Cms

    您需要确保Module 已正确注册。

    然后创建文件:app/code/<Vendor>/Cms/Model/PageLayout.php

    <?php
    
    namespace <Vendor>\Cms\Model;
    
    use Magento\Cms\Model\Page\Source\PageLayout as BasePageLayout;
    
    class PageLayout extends BasePageLayout{
    
        public function toOptionArray()
        {
            $options = parent::toOptionArray();
            $remove = [
                "empty",
                "1column",
                "2columns-left",
                "2columns-right",
                "3columns",
            ];
    
            foreach($options as $key => $layout){
                if(in_array($layout["value"], $remove)){
                    unset($options[$key]);
                }
            }
    
            return $options;
        }
    }
    

    这将获取$options,然后根据$option['value'] 删除$remove 数组中的任何内容

    为了让这个运行,你需要覆盖app/code/Magento/Cms/view/adminhtml/ui_component/cms_page_form.xml的一部分

    为此,请创建文件:app/code/&lt;Vendor&gt;/Cms/view/adminhtml/ui_component/cms_page_form.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <fieldset name="design">
            <field name="page_layout">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object"><Vendor>\Cms\Model\PageLayout</item>
                </argument>
            </field>
        </fieldset>
    </form>
    

    我们现在告诉 ui_component 字段使用我们的新模型来检索选项。

    您也可以创建文件app/code/&lt;Vendor&gt;/Cms/view/adminhtml/ui_component/cms_page_listing.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <columns name="cms_page_columns">
            <column name="page_layout">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object"><Vendor>\Cms\Model\PageLayout</item>
                </argument>
            </column>
        </columns>
    </listing>
    

    【讨论】:

    • 除了此解决方案不会从某些部分删除那些默认页面布局选项(如类别或产品设计设置 - 但这只是在此解决方案中更新 adminhtml/ui_component 布局的问题),这工作正常。因此,我不明白,为什么这个答案没有被标记为正确?
    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多