In this topic we will show you How to Remove Block Depending on a Config Setting through 2 steps:
- Step 1: Create your events.xml
- Step 2: Create RemoveBlock.php
Step 1: Create your events.xml
Create your events.xml in events.xml file in app\code\[Name_Space]\[Your_Module]\etc
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" /> </event> </config> |
Step 2: Create RemoveBlock.php
Create RemoveBlock.php in app\code\[Name_Space]\[Your_Module]\Model\Observer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<?php 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( 'dashboard/settings/remove', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); if ($remove) { $layout->unsetElement('dashboard'); } } } } |
The steps I mention above is the shortest process for you to Remove Block Depending on a Config Setting. With this guide, you can manage the Config Setting in Magento 2 easily. Every store has a Config Setting in Magento 2 with many attributes.
Thank you for reading this post and see you in other posts from Magestore!