I want to create plugin for contact form with two custom fields first-name and last-name to send email. anyone can help with this?
Thanks in advance.
in di.xml
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Contact\Controller\Index\Post">
<plugin name="Convert_CustomContact_Plugin" type="Convert\CustomContact\Plugin\Post" sortOrder="10" disabled="false" />
</type>
</config>
In plugin
<?php
namespace Convert\CustomContact\Plugin;
use Magento\Framework\App\Request\DataPersistorInterface ;
use Magento\Framework\App\Action\Context;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Translate\Inline\StateInterface;
class Post
{
protected $inlineTranslation;
protected $_escaper;
public function __construct(
DataPersistorInterface $dataPersistor,
Context $context,
Escaper $escaper,
StateInterface $inlineTranslation
) {
$this->dataPersistor = $dataPersistor;
$this->context = $context;
$this->_escaper = $escaper;
$this->inlineTranslation= $inlineTranslation;
}
public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result)
{
// In the original method, dataPersistor is cleared if a message has been sent
// so we can use it as an indicator to whether a user can be redirected somewhere else
// or fall back to the result of the original method
$post = $subject->getRequest()->getPostValue();
if ($post) {
$this->inlineTranslation->suspend();
// $this->inlineTranslation->suspend();
$postObject = new \Magento\Framework\DataObject();
$postObject->setData($post);
$error = false;
$sender = [
'firstname' => $this->_escaper->escapeHtml($post['firstname']),
'lastname' => $this->_escaper->escapeHtml($post['lastname']),
];
}
return $result;
}
}