5

In Magento 2, you can suppress the display of a block using the remove tag

<referenceBlock name="product.info.media.image" remove="true"/>

However, this doesn't remove the block from the layout completely. If you tried to insert a new block with the name product.info.media.image, you'd get the following error

main.CRITICAL: exception 'Magento\Framework\Exception\LocalizedException' with message 'Element with ID 'product.info.media' already exists.' in /path/to/magento/framework/Data/Structure.php:151

Is there a way -- similar to Magento 1's unsetChild -- to completely remove a block.

Asking because many Magento 1 extension remove and reinsert blocks, and having a way to do this in Magento 2 would help with automated conversion tools.

1
  • good point, so we are not able anymore, to change a blocks class. It might be intended behavior since it prevents that a template suddenly throws an error because you changed the block to a completely different one. You would still be able to swap it out via di configuration but it would have at least to extend the original block Feb 20, 2016 at 7:23

3 Answers 3

5

As near I can tell, you can do do this at the PHP level, but not (as of the 2.0/GA release) at an XML level.

The layout structure object (Magento\Framework\View\Layout\Data\Structure), which is the single class that controls which elements are in, or not in, a specific request's layout, has an unsetChild and unsetElement method.

However, there doesn't appear to be any layout XML directives that allow you to pass through a raw unsetChild or unsetElement command.

2

I believe this type of procedure would be seen as redundant in Magento 2 because you can easily move the block using the move layout instruction. Whereas previously this functionality was not implemented in Magento 1.*, so you had to remove the block and then reinsert it, whereas now you can simply move it.

 <move element="product.info.media.image" destination="destination"/>

EDIT: Though, perhaps I'm missing the point of the question completely, as you are Alan Storm, and you certainly probably already know this. Haha!

3
  • +1 for good information -- the specific problem I was trying to solve here was implementing some functionality where a third party module replaces named block with its own version by unsetting, then adding with a new name. Feb 21, 2016 at 23:39
  • Ah I see. In Magento 2, does it not override it using layout instructions in 'app/code/<Vendor>/<Module_Name>/View/frontend/layout/override'? Feb 24, 2016 at 11:20
  • If I could give you another +1 I would for more good information, but that's not quite what I was after either. The problem with override you're replacing an entire layout XML file. If the file you're replacing includes many blocks using an override increase the likelihood a Magento update will break things. What I'm looking for is a way to surgically say "remove this single block, replace it with another" Feb 24, 2016 at 17:36
2

The start of this thread is a little while away, but I want to state, that at least in Magento 2.1.x there is a Method "unsetChild". It can be called with the following code:

  <action method="unsetChild">
        <argument name="block" xsi:type="string">product.info.addtocart</argument>
    </action>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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