We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

Configure Module

Create module.xml at app/code/Magemonkeys/RewriteContact/etc and add the following code inside it:

<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magemonkeys_RewriteContact',
__DIR__
);

Override di.xml File

Now, I will override di.xml file. Create di.xml at app/code/Magemonkeys/RewriteContact/etc and add following code in the dl.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoContactBlockContactForm" type="MagemonkeysRewriteContactBlockContactForm" />
</config>

Override Contact Form

It’s time to override contact form. Create ContactForm.php at app/code/Magemonkeys/RewriteContact/Block and add following code in this newly created file:

<?php
namespace MagemonkeysRewriteContactBlock;
use MagentoFrameworkViewElementTemplate;
class ContactForm extends MagentoContactBlockContactForm{

public function getText() 
{ 
return "Override Contact Block"; 
}
}

Override contact_index_index.xml

Create contact_index_index.xml at app/code/Magemonkeys/RewriteContact/view/frontend/layout and add following code in this file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">    
   <body>     
      <referenceBlock name="contactForm" remove="true"/>   
       <referenceContainer name="content">
           <block class="MagentoContactBlockContactForm" name="customContactForm" template="Magemonkeys_RewriteContact::form.phtml" />
       </referenceContainer>
   </body>
</page>

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/Magemonkeys/RewriteContact/view/frontend/templates folder.

Now, you just need to update the form.phtml by adding following code:

<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>

<form class="form contact"

     action="<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>"

     id="contact-form"

     method="post"

     data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"

     data-mage-init='{"validation":{}}'>

   <fieldset class="fieldset">
       <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Write Us') ?></span></legend><br />
       <div class="field note no-label"><?php /* @escapeNotVerified */ echo __('Jot us a note and we’ll get back to you as quickly as possible.') ?></div>
<div class="field note no-label"><?php echo $block->getText(); ?></div>
       <div class="field name required">
           <label class="label" for="name"><span><?php /* @escapeNotVerified */ echo __('Name') ?></span></label>
           <div class="control">
               <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" data-validate="{required:true}"/>
           </div>
       </div>
       <div class="field email required">
           <label class="label" for="email"><span><?php /* @escapeNotVerified */ echo __('Email') ?></span></label>
           <div class="control">
               <input name="email" id="email" title="<?php /* @escapeNotVerified */ echo __('Email') ?>" value="<?php echo $block->escapeHtml($this->helper('MagentoContactHelperData')->getPostValue('email') ?: $this->helper('MagentoContactHelperData')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
           </div>
       </div>
       <div class="field telephone">
           <label class="label" for="telephone"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label>
           <div class="control">
               <input name="telephone" id="telephone" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" value="<?php echo $block->escapeHtml($this->helper('MagentoContactHelperData')->getPostValue('telephone')) ?>" class="input-text" type="text" />
           </div>
       </div>
       <div class="field comment required">
           <label class="label" for="comment"><span><?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?></span></label>
           <div class="control">
               <textarea name="comment" id="comment" title="<?php /* @escapeNotVerified */ echo __('What’s on your mind?') ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?php echo $block->escapeHtml($this->helper('MagentoContactHelperData')->getPostValue('comment')) ?></textarea>
           </div>
       </div>
       <?php echo $block->getChildHtml('form.additional.info'); ?>
   </fieldset>
   <div class="actions-toolbar">
       <div class="primary">
           <input type="hidden" name="hideit" id="hideit" value="" />
           <button type="submit" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>" class="action submit primary">
               <span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
           </button>
       </div>
   </div>
</form>

I have added <?php echo $block->getText(); ?>  under <div class=“field note no-label”>  in order to display it in a clear and decent format.

Run Commands

Go to the root directory of your store and run the following commands:

php bin/magento module:enable Magemonkeys_RewriteContact
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento cache:clean
php bin/magento cache:flush

Result

Launch your store and go to the contact form.  You will see the desired result as below:

Fill the below form if you need any Magento relate help/advise/consulting.

With Only Agency that provides a 24/7 emergency support.

    Recent Articles
    Get a Free Quote

      Full Name

      Email Address

      Mobile Number

      Message

      Let’s initiate a discussion!!

      With Only Agency that provides a 24/7 emergency support.

        Full Name

        Email Address

        Mobile Number

        Message