8

When I read the documentation on page-layouts (http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-types.html#layout-types-page) I see that the following instructions in this XML document are allowed:

  • <container>
  • <referenceContainer>
  • <move>
  • <update>

Since <referenceBlock name="block.name" remove="true" /> isn't one of the allowed instructions how can I remove a certain element in a specific page layout?

What I want for the top-categories is a whole other page design. I don't need certain containers and blocks for this design. For these categories I only want to show child categories and not the products.

The thing I've done now is, added the following lines in my page layout: (in design\frontend\Vendor\Name-of-theme\Magento_Theme\page_layout\catalog-blocks.xml)

<move element="category.image" destination="delete"></move>
<move element="category.description" destination="delete"></move>
<move element="category.products" destination="delete"></move>
<move element="page.main.title" destination="delete"></move>

In the design\frontend\Vendor\Name-of-theme\Magento_Theme\layouts.xml I addded:

<layout id="catalog-blocks">
    <label translate="true">Catalog Blocks</label>
</layout>

I gave all the top-categories this catalog-blocks page layout in the admin.

Then in the main layout (design\frontend\Vendor\Name-of-theme\Magento_Theme\layout\default.xml) I added:

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

So the whole delete block will be deleted, with all the things I moved here, in the page layout. This works well! It achieves exactly what I want but the whole thing seems to me a little hackish.

Is this considered being a good practice? If not, what would be a good alternative?

Edit 1: Changed the remove-tag in my question with the right one to avoid confusion.

Edit 2: Some more information about the files and file-locations.

11
+50

What about:

  • creating a new container (instead of a block)
  • move your blocks to the container
  • remove the container

Something like this:

<container name="delete" htmlTag="div" htmlClass="delete"/>

<move element="category.image" destination="delete"></move>
<move element="category.description" destination="delete"></move>
<move element="category.products" destination="delete"></move>
<move element="page.main.title" destination="delete"></move>

<referenceContainer name="delete" remove="true" />
  • Yes, that is exactly what I've done. My point is: this feels weird. Is this the only workaround? Why is it that Magento didn't build this feature into the page_layouts and is there a good reason for not being able to remove blocks? – 11mb May 4 '16 at 12:27
  • @11mb it's slightly different than what you have done as in my post I'm creating and referencing a container in one single file (the page layout xml) whereas in your question you're using a block and several XML files. However that's a great question I'm not sure why Magento didn't build this feature in the page_layouts – Raphael at Digital Pianism May 4 '16 at 12:42
  • @11mb feel free to create an issue on the Magento 2 GitHub repo to address that problem, it's definitely an interesting issue and I'm sure there's a good reason behind it – Raphael at Digital Pianism May 4 '16 at 12:46
  • 1
    I see what you've have done here. I think it's a slightly better approach indeed. I will test it out and come back here. – 11mb May 4 '16 at 13:01
6

This is the best practice for remove any block or container in magento 2. You can pass name attribute inside name in below element.

<referenceBlock name="block.name" remove="true" />

You can search inside magento 2 system, you have find many places syntax same as above.

Its magento recommendation way.If attribute remove="true" then add the element to list remove from that layout.

In magento 1 <remove> element is valid but in Magento 2 you have to set syntax same as above.

  • Thx! I think I have to rephrase the question a little. Is it possible to use <referenceBlock name="block.name" remove="true" /> (I was wrong about the remove tag) in a page-layout directly since referenceBlock isn't allowed either. I still should move an element to a 'delete-container' and then delete it in the main layout file. Which seems strange to me. – 11mb May 2 '16 at 8:10
  • referenceBlock is used when already block is defined with name. otherwise its not useful. ex. <block class="Magento\Framework\View\Element\Html\Links" name="top.links"> , in this case you can use <referenceBlock name="top.links" remove="true" /> – Rakesh Jesadiya May 2 '16 at 8:39
  • I'm aware that the block should exist on beforehand. But in a page layout I can't use all the generic layout instructions as described here: devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/…. (I edited my question), so I can't use referenceBlock – 11mb May 2 '16 at 8:47
  • in which page are you working for now and which element wnat to delelte? – Rakesh Jesadiya May 2 '16 at 8:50
  • I added some more information in the question. Thanks for your support! – 11mb May 2 '16 at 9:02
1

The page layouts are only supposed to be used to create a wireframe/structure of the page and not for smaller modifications such as removing blocks.

To remove a block for only your new layout I think the best way is to create your own layout handle and make the modifications in there. For example you could create catalog_category_toplevel.xml that uses your new wireframe/layout and remove the blocks in there.

As this is more of a back-end task I don't feel comfortable enough to provide instructions for this sorry :( These two articles from Alan Storm may help you with this:

TL:DR

If you wish to do this and follow best practice you need to create a new layout handle.

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.