cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 : Remove/Unset block from controller

Magento 2 : Remove/Unset block from controller

Hello Guys,

How can i remove/unset block from controller ?

 

I need to call new block in same phtml and remove current block, I can call new block but can't able to remove existing block ?

 

Anybody can help ?????

2 REPLIES 2

Re: Magento 2 : Remove/Unset block from controller

You could replace your template xml file with the block you want.

 

If you want to remove an specific block, you will need to copy/paste the content of the action and remove what you do not need/want.

Lets suppose you want to remove the block

Magento\Cookie\Block\RequireCookie

from the product page.


You need to create a new layout file in your module: e.g.:
vendor/module/view/frontend/layout/catalog_category_view.xml

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml">
<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
<container name="category.product.list.additional" as="additional" />
<block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
<block class="Magento\Framework\View\Element\Template" name="category.product.type.details.renderers.default" as="default"/>
</block>
<block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="category.product.addto" as="addto">
<block class="Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare"
name="category.product.addto.compare" as="compare"
template="Magento_Catalog::product/list/addto/compare.phtml"/>
</block>
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
</block>
</referenceContainer>
<referenceBlock name="page.main.title">
<arguments>
<argument name="id" xsi:type="string">page-title-heading</argument>
<argument name="add_base_attribute_aria" xsi:type="string">page-title-heading toolbar-amount</argument>
</arguments>
<block class="Magento\Catalog\Block\Category\Rss\Link" name="rss.link" template="Magento_Catalog::category/rss.phtml"/>
</referenceBlock>
</body>
</page>

 

Re: Magento 2 : Remove/Unset block from controller

Hello, Thanks for responding back.

 

You didn't read my question properly.

 

I need to remove block from controller execute method.

 

I have added new block, But when new block added i need to remove previous block.

 

Check my below code. You will get an idea what i am looking for.

 

 case "1":
		    $layout = $this->_view->loadLayout();
		   
			$resultPage = $this->_resultPageFactory->create();
			
			$block = $resultPage->getLayout()
                ->createBlock('Inchoo\Helloworld\Block\AcceptedGroupRequest')
                ->setTemplate('Inchoo_Helloworld::acceptedGroupRequest.phtml')
                ->toHtml();
               $this->getResponse()->appendBody($block);
                
		       break;