从Magento 2中的布局中删除块


35

在Magento 1中,我可以通过将其添加到布局块中来删除布局文件添加的块

<remove ="block_id_here" />

Magento 2我该怎么做?
作为一个实践练习,假设我有自己的模块,我想从该模块中删除管理仪表板页面中的仪表板块。使用以下命令
添加该块app/code/Magento/Backend/view/adminhtml/layout/adminhtml_dashboard_index.xml

<referenceContainer name="content">
    <block class="Magento\Backend\Block\Dashboard" name="dashboard"/>
</referenceContainer>

我假设我需要view/adminhtml/layout/adminhtml_dashboard_index.xml在模块中创建文件,但是我需要在文件中添加什么呢?

Answers:


70

在最新版本的Magento2中,remove方法现在为:

<referenceBlock name="block_name" remove="true"/>

例:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="block_name" remove="true"/>
    </body>
</page>

知道这一点很重要,以防万一您要尝试做的不仅仅是删除元素。将名称空间更改为布局,而不是将其更改为page_configuration您可能无法做的所有事情。


这对我有用。但是奇怪的是,在devdocs.magento.com/guides/v2.0/frontend-dev-guide/themes/…上说明的示例实际上使用<remove />-tag。文档中有错误?
Giel Berkers '16

@GielBerkers很可能是文档中的错误-确保让他们知道github.com/magento/devdocs
艾伦·斯托姆

我该怎么做一个phtml文件
Waqar Ali


0

假设您要从成功页面中删除标题栏。首先,您需要找到负责该特定页面的xml,在本例中将是vendor/magento/module-checkout/view/frontend/layout/checkout_onepage_success.xml

在此文件中,您将拥有以下内容:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Success Page</title>
    </head>
    <body>
        <referenceBlock name="page.main.title">
            <block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success.print.button" template="Magento_Checkout::button.phtml"/>
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Thank you for your purchase!</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success" template="Magento_Checkout::success.phtml" cacheable="false">
                <container name="order.success.additional.info" label="Order Success Additional Info"/>
            </block>
            <block class="Magento\Checkout\Block\Registration" name="checkout.registration" template="Magento_Checkout::registration.phtml" cacheable="false"/>
        </referenceContainer>
    </body>
</page>

现在,您需要在主题中扩展此xml,app/design/frontend/.../.../Magento_Checkout/layout/checkout_onepage_success.xml 并在其中引用需要删除page.main.title和添加的代码块,remove="true"如下所示:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
    </head>
    <body>
        <referenceBlock name="page.main.title" remove="true" />
    </body>
</page>

如果您希望从所有cms页面中删除特定的块,则可以通过扩展vendor/magento/module-theme/view/frontend/layout/default.xml 主题文件夹中的默认xml来实现,app/design/frontend/.../.../Magento_Theme/layout/default.xml如下所示:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="page.main.title" remove="true" />
    </body>
</page>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.