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;
}
}
}
Then clear cache.
NOte:This module is tested
Best Answer
Let's say I have a custom block
Rkt\StackExchange\Block\Main
defined atapp/code/Rkt/StackExchange/Block/Main.php
.Now include this block within your CMS Page like this:
Important points to note here is, I included the
page_identifier
in the bock declaration. The page identifier is your URL key for the CMS page. Other than that I have specified the block type and block template. So let us define both of these files.The Template File
File:
app/code/Rkt/StackExchange/view/frontend/templates/form.phtml
As you can see, we are calling the
getPage()
method of block class to render the cms page content.The Block Class
File:
app/code/Rkt/StackExchange/Block/Main.php
In the
getPage()
method, we are collecting the identifier, then using CMS Page helper to retrieve the content of the cms page.Better Solution
Though the solution above given works for your requirement, I think we are making things complex here. I think the solution put forward by @magento68 seems to be a good solution here.
Or create a static block with the CMS page content that you wish to add in your current CMS page, then just invoke the static block in the CMS Page. This will be simple and easy to do.