{"id":11893,"date":"2020-12-10T15:02:46","date_gmt":"2020-12-10T09:32:46","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11893"},"modified":"2020-12-11T15:40:22","modified_gmt":"2020-12-11T10:10:22","slug":"remove-block-layout-specific-condition-magento-2","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/remove-block-layout-specific-condition-magento-2\/","title":{"rendered":"How to Remove Block From Layout on Specific Condition in Magento 2"},"content":{"rendered":"<p>In this post I am going to explain how to remove block from layout on specific condition Magento 2.<\/p>\n<p>If you have a specific condition to add\/remove block then you can achieve it by observer.<\/p>\n<p>Here I have given the example to remove search block.<\/p>\n<p>First you need to create event.xml file on below path.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/etc\/frontend\/events.xml<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\"&gt;\r\n    &lt;event name=\"layout_generate_blocks_after\"&gt;\r\n        &lt;observer name=\"remove_search_block\" instance=\"&lt;vendor&gt;\\&lt;module&gt;\\Observer\\RemoveBlockSearch\" \/&gt;\r\n    &lt;\/event&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>Now, create observer on below path.<\/p>\n<p><strong>Path: app\/code\/&lt;vendor&gt;\/&lt;module&gt;\/Observer\/RemoveBlockSearch.php<\/strong><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\nnamespace &lt;vendor&gt;\\&lt;module&gt;\\Observer;\r\n\r\nuse Magento\\Framework\\Event\\Observer;\r\n\r\nclass RemoveBlockSearch implements \\Magento\\Framework\\Event\\ObserverInterface\r\n{\r\n\r\n    public function execute(Observer $observer)\r\n    {\r\n        $layout = $observer-&gt;getLayout();\r\n        $block = $layout-&gt;getBlock('top.search');\r\n\r\n        if ($block) {\r\n            \/\/ Here you can add yout condition\r\n            $layout-&gt;unsetElement('top.search');\r\n        }\r\n    }\r\n}<\/pre>\n<p>After adding above code, search block will removed from the Header.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1775803586\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to remove block from layout on specific condition Magento 2. If you have a specific condition to add\/remove block then you can achieve it by observer. Here I have given the example to remove search block. First you need to create event.xml file on below path&#8230;. <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/remove-block-layout-specific-condition-magento-2\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":12206,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[729,9,10,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11893"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11893"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11893\/revisions"}],"predecessor-version":[{"id":12207,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11893\/revisions\/12207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/12206"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}