{"id":127375,"date":"2018-07-16T05:37:06","date_gmt":"2018-07-16T05:37:06","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=127375"},"modified":"2023-01-09T10:29:42","modified_gmt":"2023-01-09T10:29:42","slug":"ways-to-create-unset-blocks-in-magento","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/","title":{"rendered":"Ways to set \/ unset  Blocks  in Magento"},"content":{"rendered":"<p>Today I am going to share how we can Add Remove Block in Magento 2.<\/p>\n<p>I hope you have basic knowledge about how to create module in Magento.<br \/>\nIf you are new to Magento then first go through this <a href=\"https:\/\/webkul.com\/blog\/create-hello-module-in-magento2\/\">link<\/a>.<\/p>\n<p>Magento Follows MVC design pattern and I found that people are generally confused about layout and view system.<br \/>\nand block is a part of the View system. In magento View is separated into two parts, One is Block and another is Template.<\/p>\n<p>What is Block ?<br \/>\nBlock is a simple Object tied to Template file.<\/p>\n<p>What is Template ?<br \/>\nTemplate is a Raw phtml file (having the extention .phtml) contain php and html code. In phtml(template) file $this keyword keeps the reference of the template&#8217;s Block Object.<\/p>\n<p>We can perform block nesting as well. Block into Block.<br \/>\nBasically block inside block inside block is how Magento html layout is create for page.<br \/>\nFor performing Nesting we use a method getChildHtml method.<\/p>\n<p>getChildHtml help us to call any block in any place in particular page scope.<br \/>\nExample :<\/p>\n<p>Call specific block $this-&gt;getChildHtml(&#8220;head&#8221;);<\/p>\n<p>All child Blocks $this-&gt;getChildHtml();<\/p>\n<p>There are lots of terms that you should know, like layout packaging, references , handles etc,<\/p>\n<p>but I can not cover all in one \u00a0as I need to explain a \u00a0thing that you may need when you are creating any module \u00a0and situation came and you want to remove the block from the layout and add new block in layout.<\/p>\n<p>To add\/remove block in layout we need to get the reference of the layout Object. We use getLayout() method to get it&#8217;s reference.<\/p>\n<p>There are generally two ways to remove the block from the layout :<br \/>\n1. XML<br \/>\n2. PHP Code.<\/p>\n<p>First let me cover the XML part .<br \/>\nRemove \/ add Block :<\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\"?&gt;\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"2columns-right\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\n    &lt;body&gt;\n        &lt;referenceBlock name=\"head\" remove=\"true\" \/&gt;        &lt;!--remove block --&gt;\n        &lt;referenceBlock name=\"content\"&gt;              &lt;!--add block --&gt;\n            &lt;block \n                class=\"&lt;CustomVendor\\CustomModule\\Block\\CustomBlock&gt;\"\n                name=\"&lt;myblock-name&gt;\"\n                template=\"&lt;CustomVendor_CustomModule::test.phtml&gt;\"\/&gt;\n        &lt;\/referenceBlock&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n<p>Add\/Remove Block \u00a0(PHP):<\/p>\n<pre class=\"brush:php\">$layout = $this-&gt;getLayout();\n\n$block  = $layout-&gt;getBlock('content');\n \nif ($block) {\n   \/\/ you can use any condition here.\n   \/\/ remove block\n    $block-&gt;unsetChild(\"&lt; child block name &gt;\");\n   \/\/Or\n   $layout-&gt;unsetElement('&lt; child block name &gt;');\n}\n\n\/\/ yo can get parent block as well from this layout.\n$parentBlockName = $layout-&gt;getParentName('&lt;child block name&gt;')\n$parentBlock = $layout-&gt;getBlock($parentBlockName);\n\n\/\/ create new block \n$layout-&gt;createBlock(\"&lt;Magento\\&lt;Theme&gt;\\Block\\Test&gt;\");\n<\/pre>\n<p><strong>Note :<\/strong> you can write the above code in block as well as in Template , it works in both places.<\/p>\n<p><strong>Note<\/strong> : Best Practice, we should use this code in Block&#8217;s prepareLayout() function.<\/p>\n<p><strong>Situation<\/strong> : When to use in Template file<br \/>\nExample : parent block has used the function getChildHtml() at the bottom and you want your block to be tied in between(middle of the page\/block).<\/p>\n<p><strong>Scenario<\/strong> : if you will add the line after overriding the template in between the Template, so the same block will come twice so you have to remove the bottom occurrence of the block.<\/p>\n<p>Few more function in view object :<br \/>\n1. setTemplate($template)<br \/>\n2. toHtml()<br \/>\n3. getNameInLayout()<br \/>\nThis code will help you to come out of this situation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I am going to share how we can Add Remove Block in Magento 2. I hope you have basic knowledge about how to create module in Magento. If you are new to Magento then first go through this link. Magento Follows MVC design pattern and I found that people are generally confused about layout <a href=\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":170,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,1149,302,13],"tags":[6781,6783,6785,256,6784,6782,775],"class_list":["post-127375","post","type-post","status-publish","format-standard","hentry","category-magento","category-magento1-9","category-magento2","category-php","tag-add","tag-block","tag-handles","tag-layout","tag-references","tag-remove","tag-template"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ways to set \/ unset Blocks in Magento - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ways to set \/ unset Blocks in Magento - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Today I am going to share how we can Add Remove Block in Magento 2. I hope you have basic knowledge about how to create module in Magento. If you are new to Magento then first go through this link. Magento Follows MVC design pattern and I found that people are generally confused about layout [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-16T05:37:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-09T10:29:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prabhat Rawat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prabhat Rawat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\"},\"author\":{\"name\":\"Prabhat Rawat\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f\"},\"headline\":\"Ways to set \/ unset Blocks in Magento\",\"datePublished\":\"2018-07-16T05:37:06+00:00\",\"dateModified\":\"2023-01-09T10:29:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\"},\"wordCount\":424,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"add\",\"block\",\"handles\",\"layout\",\"references\",\"remove\",\"template\"],\"articleSection\":[\"magento\",\"magento1.9\",\"Magento2\",\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\",\"url\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\",\"name\":\"Ways to set \/ unset Blocks in Magento - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-07-16T05:37:06+00:00\",\"dateModified\":\"2023-01-09T10:29:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ways to set \/ unset Blocks in Magento\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f\",\"name\":\"Prabhat Rawat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prabhat Rawat\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prabhat-rawat763\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ways to set \/ unset Blocks in Magento - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/","og_locale":"en_US","og_type":"article","og_title":"Ways to set \/ unset Blocks in Magento - Webkul Blog","og_description":"Today I am going to share how we can Add Remove Block in Magento 2. I hope you have basic knowledge about how to create module in Magento. If you are new to Magento then first go through this link. Magento Follows MVC design pattern and I found that people are generally confused about layout [...]","og_url":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-07-16T05:37:06+00:00","article_modified_time":"2023-01-09T10:29:42+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prabhat Rawat","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prabhat Rawat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/"},"author":{"name":"Prabhat Rawat","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f"},"headline":"Ways to set \/ unset Blocks in Magento","datePublished":"2018-07-16T05:37:06+00:00","dateModified":"2023-01-09T10:29:42+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/"},"wordCount":424,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["add","block","handles","layout","references","remove","template"],"articleSection":["magento","magento1.9","Magento2","php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/","url":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/","name":"Ways to set \/ unset Blocks in Magento - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-07-16T05:37:06+00:00","dateModified":"2023-01-09T10:29:42+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/ways-to-create-unset-blocks-in-magento\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Ways to set \/ unset Blocks in Magento"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f","name":"Prabhat Rawat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prabhat Rawat"},"url":"https:\/\/webkul.com\/blog\/author\/prabhat-rawat763\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/127375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/170"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=127375"}],"version-history":[{"count":8,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/127375\/revisions"}],"predecessor-version":[{"id":363738,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/127375\/revisions\/363738"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=127375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=127375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=127375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}