Reading list Switch to dark mode

    Ways to set / unset Blocks in Magento

    Updated 9 January 2023

    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 and view system.
    and block is a part of the View system. In magento View is separated into two parts, One is Block and another is Template.

    What is Block ?
    Block is a simple Object tied to Template file.

    What is Template ?
    Template 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’s Block Object.

    Searching for an experienced
    Magento Company ?
    Find out More

    We can perform block nesting as well. Block into Block.
    Basically block inside block inside block is how Magento html layout is create for page.
    For performing Nesting we use a method getChildHtml method.

    getChildHtml help us to call any block in any place in particular page scope.
    Example :

    Call specific block $this->getChildHtml(“head”);

    All child Blocks $this->getChildHtml();

    There are lots of terms that you should know, like layout packaging, references , handles etc,

    but I can not cover all in one  as I need to explain a  thing that you may need when you are creating any module  and situation came and you want to remove the block from the layout and add new block in layout.

    To add/remove block in layout we need to get the reference of the layout Object. We use getLayout() method to get it’s reference.

    There are generally two ways to remove the block from the layout :
    1. XML
    2. PHP Code.

    First let me cover the XML part .
    Remove / add Block :

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-right" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="head" remove="true" />        <!--remove block -->
            <referenceBlock name="content">              <!--add block -->
                <block 
                    class="<CustomVendor\CustomModule\Block\CustomBlock>"
                    name="<myblock-name>"
                    template="<CustomVendor_CustomModule::test.phtml>"/>
            </referenceBlock>
        </body>
    </page>

    Add/Remove Block  (PHP):

    $layout = $this->getLayout();
    
    $block  = $layout->getBlock('content');
     
    if ($block) {
       // you can use any condition here.
       // remove block
        $block->unsetChild("< child block name >");
       //Or
       $layout->unsetElement('< child block name >');
    }
    
    // yo can get parent block as well from this layout.
    $parentBlockName = $layout->getParentName('<child block name>')
    $parentBlock = $layout->getBlock($parentBlockName);
    
    // create new block 
    $layout->createBlock("<Magento\<Theme>\Block\Test>");
    

    Note : you can write the above code in block as well as in Template , it works in both places.

    Note : Best Practice, we should use this code in Block’s prepareLayout() function.

    Situation : When to use in Template file
    Example : 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).

    Scenario : 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.

    Few more function in view object :
    1. setTemplate($template)
    2. toHtml()
    3. getNameInLayout()
    This code will help you to come out of this situation.

    . . .

    Leave a Comment

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


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home