4

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.

  • 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 – David Verholen Feb 20 '16 at 7:23
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!

  • +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. – Alan Storm Feb 21 '16 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'? – Gareth Daine Feb 24 '16 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" – Alan Storm Feb 24 '16 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, privacy policy and cookie policy

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