100% OFF! On Managed Magento Hosting Upto 2 Months. Use Coupon: MGT25
Avail Now

How to Override Block in Magento 2

I have received several queries from the readers about Magento override of blocks. This is a valid question as editing the core files is not a recommended practice. Today’s tutorial is an attempt to answer these questions.

I am going to discuss how to override Magento 2 blocks, but before starting the tutorial, let me answer a crucial question: what is a block?

Defining a Magento Block

The PHP classes used to connect (or create a link between) layout and templates are called Blocks. As a class, it is a set of methods that handle and controls the Magento UI Blocks. It has the following features:

  • A block can have more blocks (nested blocks).
  • A block can be placed on different pages and within other blocks.

Now how would you rewrite a Magento 2 block?

To illustrate the process, I will override the Magento 2 Contact Form block. For this, I need to create a custom module to override Magento 2 block. If you are not familiar with the process, I highly recommend you check out this guide: How to Create a Module in Magento 2.

Let’s begin!

Create Directories

directories structure

Configure Module

Create module.xml in app/code/Magenticians/Modulecontact/etc and add the following code to it:


Register the Module

Create registration.php in app/code/Magenticians/Modulecontact and add this code to it:

Override di.xml File

Now I will override di.xml file. For this create di.xml in app/code/Magenticians/Modulecontact/etc and add this code to it:

As you can see in the <preference for=””> tag,I have mentioned the block I want to override. Similarly, in <preference type=””> , I have mentioned where I would override it.

Override Contact Form

It’s time to actually override contact form.
Create ContactForm.php in app/code/Magenticians/Modulecontact/Block and add this code in it:

As you can see in the above code snippet, I have created a function getText().

The block is now overridden. However, to display its output at the frontend, I would need a template file. To do so, I will override two files, form.phtml and contact_index_index.xml. But before that, I would override the contact_index_index.xml

Override contact_index_index.xml

This is the Magento 2 layout xml file. To overriide it, create contact_index_index.xml in app/code/Magenticians/Modulecontact/view/frontend/layout and add this code to it:

In the above code snippet, I have added  <referenceBlock name="contactForm" remove="true"/>  to remove the original contact form so that I can show only the new overridden Magento Contact Form block.

Override form.phtml

Go to vendor/magento/module-contact/view/frontend/templates from the root directory of your store and copy the form.phtml file to app/code/Magenticians/Modulecontact/view/frontend/templates. Now you just need to update the file by adding this line:

<?php echo $block->getText(); ?>

The final form.phtml will looks like this:

As you can see I have added  <?php echo $block->getText(); ?>  under <div class="field note no-label"> in order to display it clearly and prominently.

Run Commands

Everything is now in place and the final step requires you to run the following commands. For this go back to the root folder and then run these commands:

The Result of the Overridden Block

Go to the store’s frontend and go to the contact form. In the following screenshot, you can see the text “Override Text”. This indicates I have successfully overridden the Magento 2 Contact Form block.

result block override magento 2

Get $25 Free Credit on Managed Magento Hosting!

Use Coupon: MGT25

Final Words

After going through this Magento override block guide, you should be able to override Magento 2 blocks without any issues. You can override other Magento blocks with a similar approach and utilize them according to your needs. Want to discuss something regarding this guide? Just use the comment section below and I will get back to you!

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.