35

In Magento 1 I could remove a block added by a layout file by adding this in my layout block

<remove ="block_id_here" />

How can I do the same for Magento 2?
As a practical exercise, let's say I have my own module from which I want to remove dashboard block from the admin dashboard page.
The block is added from app/code/Magento/Backend/view/adminhtml/layout/adminhtml_dashboard_index.xml using this:

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

I assume I need to create the file view/adminhtml/layout/adminhtml_dashboard_index.xml in my module, but what do I need to put in it?

65

In more recent versions of Magento2, the remove method is now:

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

Example:

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

This is important to know in case you are trying to do something more than just remove an element. Changing the namespace to layout instead of page_configuration may not allow you to do everything you are needing to do.

8

In the most recent dev branch of magento 2, try create view/adminhtml/layout/adminhtml_dashboard_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-dashboard" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="dashboard" remove="true"/>

Source https://github.com/magento/magento2/search?l=xml&q=remove&utf8=%E2%9C%93

0

So let's say you want to remove the title block from the success page. First you will need to find the xml that is responsible for that specific page in our case it will be vendor/magento/module-checkout/view/frontend/layout/checkout_onepage_success.xml

And in this file you will have the following content:

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

You will now need to extend this xml in your theme app/design/frontend/.../.../Magento_Checkout/layout/checkout_onepage_success.xml And inside it reference the block you need to remove page.main.title and add remove="true" like so:

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

If you wish to remove a specific block from all cms pages you can achieve this by extending the default xml vendor/magento/module-theme/view/frontend/layout/default.xml in you theme folder app/design/frontend/.../.../Magento_Theme/layout/default.xml like so:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.