4

I created a new page with the CMS. I paste this in editor:

{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Theme::html/contact_form.phtml"}}

Then I overrided the contact_form.phtml successfully for my theme under

app/design/fontend/MyThemes/2016/Magento_Theme/templates/html/

Submitting the form send to this controller vendor/module-contact/Controller/Index/Post.php

Route used in this controller is $this->_redirect('contact/index'); which is not the page I created with the cms.

My Question now is, how to override the controller?

I tried by creating the file under app/code/module-contact/Controller/Index/Post.php, but no success.

<?php    
namespace Magento\Contact\Controller\Index;

class Post extends \Magento\Contact\Controller\Index
{
    public function execute()
    {
        die('pass here?');
    }
}
  • can you please put our override code? – Amit Bera Jun 7 '16 at 18:59
  • @arsenik how to change the subject for contact us form – Nagaraju Kasa Jul 10 '18 at 7:26
5

You need to create small module for this.

Using di.xml can rewrite the controller Post.php

First ,Create module registration file registration.php at app\code\VendorName\ModuleName. Code is below:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ModuleName',
    __DIR__
);

Then create module main config module.xml file at app\code\VendorName\ModuleName\etc. Code is below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_ModuleName" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Contact"/>
        </sequence>
    </module>
</config>

3rd, create di.xml where you define configuration that which class will rewrite and which class rewriting and di.xml located at app\code\VendorName\ModuleName\etc and code is below:

<?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\Controller\Index\Post" type="VendorName\ModuleName\Controller\Index\Post" />
</config>

4th, write the rewrite class which located at Post.php app\code\VendorName\ModuleName\Controller\Index.

<?php
namespace VendorName\ModuleName\Controller\Index;

class Post extends \Magento\Contact\Controller\Index\Post
{
    /**
     * Post user question
     *
     * @return void
     * @throws \Exception
     */
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!$post) {
            $this->_redirect('*/*/');
            return;
        }

        $this->inlineTranslation->suspend();
        try {
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($post);

            $error = false;

            if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                $error = true;
            }
            if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
                $error = true;
            }
            if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }
            if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                $error = true;
            }
            if ($error) {
                throw new \Exception();
            }

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->setReplyTo($post['email'])
                ->getTransport();

            $transport->sendMessage();
            $this->inlineTranslation->resume();
            $this->messageManager->addSuccess(
                __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
            );
            $this->_redirect('YOUR_CMS_PAGE_Identifier'); // change here 
            return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addError(
                __('We can\'t process your request right now. Sorry, that\'s all we know.')
            );
            $this->_redirect('YOUR_CMS_PAGE_Identifier');  // change here 
            return;
        }
    }
}
  • Now you need to enable php bin/magento module:status

    php bin/magento module:enable VendorName_ModuleName for enable the module.

  • and run php bin/magento setup:upgrade for enabled modules are properly registered.

Then clear cache.

NOte:This module is tested

  • VendorName\ModuleName can be MyThemes\2016? – arsenik Jun 7 '16 at 19:55
  • yes it can but note that location should be app/code – Amit Bera Jun 7 '16 at 20:04
  • what is AmitBera_CustomContacts ? in module.xml – arsenik Jun 7 '16 at 20:36
  • Answer updated.. now check – Amit Bera Jun 7 '16 at 20:39
  • hi @AmitBera i need one small help how to set custom subject for contact us form in the controller? – Nagaraju Kasa Jul 10 '18 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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