How to Override Block in Magento 2

4
2

Defining a Magento Block

           <block class=“MagentoContactBlockContactForm” name=“customContactForm” template=“Magenticians_Modulecontact::form.phtml” />

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

* Copyright © 2013-2017 Magento, Inc. All rights reserved.


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

Create Directories

43

Configure Module

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

The Result of the Overridden Block

10
               <input name=“name” id=“name” title=<?php /* @escapeNotVerified */ echo __(‘Name’) ?> value=<?php echo $block->escapeHtml($this->helper(‘MagentoContactHelperData’)->getPostValue(‘name’) ?: $this->helper(‘MagentoContactHelperData’)->getUserName()) ?> class=“input-text” type=“text” datavalidate=“{required:true}”/>

<page xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” layout=“1column” xsi:noNamespaceSchemaLocation=“urn:magento:framework:View/Layout/etc/page_configuration.xsd”>    
41

Final Words

35

Related Reading

Subscribe Newsletter

Now how would you rewrite a Magento 2 block?