I have created a custom module app/code/Demo/Mymodule and I want to override Contact Form Block.

After configuration and registration of module, I have Created di.xml in app/code/Demo/Mymodule/etc

<?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="Demo\Mymodule\Block\ContactForm" />

Then, I have created ContactForm.php in app/code/Demo/Mymodule/Block

namespace Demo\Mymodule\Block;
use Magento\Framework\View\Element\Template;
class ContactForm extends \Magento\Contact\Block\ContactForm{
public function getFormAction()    {
echo "Demo Text";
return $this->getUrl('contact/index/post', ['_secure' => true]);

I have run all the commands like:

php bin/magento setup:upgrade
php bin/magento cache:clean

But, I am not getting output (Demo Text) on contact page

  • What you want to change in your custom block? Commented Aug 8, 2017 at 11:46
  • Right now, I just want to add text (Demo Text) which I have written in ContactForm.php. I just want to check whether the block is overridden or not Commented Aug 8, 2017 at 11:48

1 Answer 1


I have checked your code, it's working

You can see "Demo Text" before in form action. You can view it by inspect element.

Contact form action like this in inspect element

<form class="form contact" action="Demo Texthttp://

Also you can test it by write exit; just after "Demo Text" in your custom block.

public function getFormAction()    {
echo "Demo Text";
return $this->getUrl('contact/index/post', ['_secure' => true]);

If you show text on contact form, see below solution for it

Copy file from


and put it to


You can change contact us form from this file, write a code as per you need.

  • Where to write exit? I can't get it Commented Aug 8, 2017 at 11:57
  • see my updated answer Commented Aug 8, 2017 at 11:59
  • Actually I want to show text on contact form Commented Aug 8, 2017 at 12:01
  • No need to modify core block. You can write directly in form.phtml file. Commented Aug 8, 2017 at 12:03
  • If I want to show text on contact form using ContactForm.php (Block), then how can I do that? Commented Aug 8, 2017 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.