51

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

Rafael Corrêa Gomes
13.9k15 gold badges92 silver badges190 bronze badges
asked Aug 31, 2015 at 14:30

3 Answers 3

100

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.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
answered Sep 11, 2015 at 14:51
3
  • This worked for me. The odd thing is however, that the example stated on devdocs.magento.com/guides/v2.0/frontend-dev-guide/themes/… actually uses the <remove />-tag. Error in the documentation? Commented Feb 12, 2016 at 15:51
  • @GielBerkers Very likely an error in the documentation -- be sure to let them know github.com/magento/devdocs Commented Feb 20, 2016 at 0:18
  • How can i do this a phtml file Commented May 10, 2019 at 7:06
11

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

answered Sep 1, 2015 at 17:45
1

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>
answered Nov 9, 2018 at 14:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.