Magento 2删除布局选项

4
我正在为Magento 2创建自定义主题。 在页面、类别、产品等中选择“设计”选项卡时,我有以下选项:
1列 带左侧栏的2列 带右侧栏的2列 3列
我已经知道如何将我的自定义布局添加到此选项集中。
我的问题是:如何删除(或在紧急情况下隐藏)不必要的核心布局?例如,我的主题根本不需要3列布局,因此我不希望这是一个选项。
1个回答

1

好的,这是您需要做的:

创建一个新的 Module: app/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/<Vendor>/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/<Vendor>/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布局只是一个问题),它可以正常工作。因此,我不明白为什么这个答案没有被标记为正确的呢? - Toms Bugna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接