Create module.xml at app/code/Magemonkeys/RewriteContact/etc and add the following code inside it:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_RewriteContact', __DIR__ ); |
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.
1 2 3 4 |
<?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="Magento\Contact\Block\ContactForm" type="Magemonkeys\RewriteContact\Block\ContactForm" /> </config> |
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:
1 2 3 4 5 6 7 8 9 10 |
<?php namespace Magemonkeys\RewriteContact\Block; use Magento\Framework\View\Element\Template; class ContactForm extends \Magento\Contact\Block\ContactForm{ public function getText() { return "Override Contact Block"; } } |
Create contact_index_index.xml at app/code/Magemonkeys/RewriteContact/view/frontend/layout and add following code in this file:
1 2 3 4 5 6 7 8 9 |
<?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="Magento\Contact\Block\ContactForm" name="customContactForm" template="Magemonkeys_RewriteContact::form.phtml" /> </referenceContainer> </body> </page> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?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('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->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('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->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('Magento\Contact\Helper\Data')->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('Magento\Contact\Helper\Data')->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.
Go to the root directory of your store and run the following commands:
1 2 3 4 5 6 |
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 |
Launch your store and go to the contact form. You will see the desired result as below:
[crayon-63e09a5cc3678052600515/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.