How to eliminate block depending on a config setting in Magento 2

Image result for magento 2

By using two steps we will be able to remove block depending on a config setting in Magento 2. The following steps are:

Step 1: create your Event.xml

Step 2: create a remove block .php

Create your Event.xml

Construct your event.xml in Event.xml file in

 <?xml version="1.0"?>

 <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

 <event name="layout_generate_blocks_after">

  <observer name="remove_block" instance="[Name_Space]\[Your_Module]\Model\Observer\RemoveBlock" />



Create a remove block.php

Construct your remove block.php by


 namespace [Name_Space]\[Your_Module]\Model\Observer;

 use Magento\Framework\Event\Observer;

 use Magento\Framework\Event\ObserverInterface;

 class RemoveBlock implements ObserverInterface


 protected $_scopeConfig;

 public function __construct(

  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig

 ) {

 $this->_scopeConfig = $scopeConfig;


 public function execute(Observer $observer)


  /** @var \Magento\Framework\View\Layout $layout */

  $layout = $observer->getLayout();

 $block = $layout->getBlock('dashboard');

 if ($block) {

 $remove = $this->_scopeConfig->getValue(




 if ($remove) {






The above mentioned steps are the shortest process to remove a block depending on a config setting .The following steps can manage the config setting in Magento 2 effortlessly.

Last Update: July 17, 2018  

February 6, 2018   804   Nandini Ramachandran    Java Script    
Total 1 Votes:

Tell us how can we improve this post?

+ = Verify Human or Spambot ?


  • I would read more tutorials about removing block depending on a configuration setting in Magento 2. But this is the first tutorial that explains it in a simple and easy way.- Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *