I'm trying to remove a block from a certain page (be it frontend or backend) but only if a certain config flag is set to true
.
Let's take an example.
I want to remove the block with the name dashboard
from the admin dashboard.
The block is defined in adminhtml_dashboard_index.xml
file from the Magento_Backend
module:
<referenceContainer name="content">
<block class="Magento\Backend\Block\Dashboard" name="dashboard"/>
</referenceContainer>
Thanks to Adam's answer I can do this in the adminhtml_dashboard_index.xml
<body>
<referenceBlock name="dashboard" remove="true" />
</body>
But I want to take it up a notch and remove this block only if the config setting with the path dashboard/settings/remove
has the value 1
.
A layout xml approach would be awesome, but I will take an observer approach also.
Best Answer
I couldn't find a way to do this with layout either but here is an example of a way you can do it with observers (providing they extend the Template block)...
Create your events.xml in etc/events.xml
Create the observer
Basically the _toHtml checks to see if there is a template. If there isn't it returns ''.
EDIT
After some more digging i have found a way to do this further up the chain.
And the observer...