Question

I am overriding contact us from this tutorial but after running am getting following error :

1 exception(s):
Exception #0 (Exception): Notice: Undefined property: Mycontct\Person\Controller\Magento\Contact\Index\Post\Interceptor::$inlineTranslation in /home/fphonour/www/olympics2020.net/app/code/Mycontct/Person/Controller/Magento/Contact/Index/Post.php on line 23

What am I missing?

Post controller :

<?php

namespace Mycontct\Person\Controller\Magento\Contact\Index;

use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class Post extends \Magento\Contact\Controller\Index\Post
{

    private $dataPersistor;

    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();

                    $message = __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.');

                    $this->getDataPersistor()->clear('contact_us');
                    echo $message;
                    //$this->_redirect('contact/index');
                    return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $message = 
                    __('We can\'t process your request right now. Sorry, that\'s all we know.');

            $this->getDataPersistor()->set('contact_us', $post);
            //$this->_redirect('contact/index');
            echo $message;
            return;
        }
    }

    private function getDataPersistor()
    {
        if ($this->dataPersistor === null) {
            $this->dataPersistor = ObjectManager::getInstance()
            ->get(DataPersistorInterface::class);
        }

        return $this->dataPersistor;
    }
}       
Was it helpful?

Solution

You need to add \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, in your Controller or remove $this->inlineTranslation->suspend(); from Controller file.

Just copy constructor method from : \Magento\Contact\Controller\Index\Post and add your new : \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,.

Also, add :

/**
 * @var \Magento\Framework\Translate\Inline\StateInterface
 */
protected $inlineTranslation;

In your file before constructor method.

Your code should be like this:

<?php

namespace Mycontct\Person\Controller\Magento\Contact\Index;

use Magento\Framework\App\Action\Context;
use Magento\Contact\Model\ConfigInterface;
use Magento\Contact\Model\MailInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use \Magento\Framework\Translate\Inline\StateInterface;
class Post extends \Magento\Contact\Controller\Index\Post
{
    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @var Context
     */
    private $context;

    /**
     * @var MailInterface
     */
    private $mail;

    /**
     * @var LoggerInterface
     */
    private $logger;

    protected $inlineTranslation;
    public function __construct(
        Context $context,
        ConfigInterface $contactsConfig,
        MailInterface $mail,
        DataPersistorInterface $dataPersistor,
        LoggerInterface $logger = null,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
    ) {
        parent::__construct($context, $contactsConfig,$mail,$dataPersistor);
        $this->inlineTranslation = $inlineTranslation;
        $this->context = $context;
        $this->mail = $mail;
        $this->dataPersistor = $dataPersistor;
        $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
    }
    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();

            $message = __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.');

            $this->getDataPersistor()->clear('contact_us');
            echo $message;
            //$this->_redirect('contact/index');
            return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $message =
                __('We can\'t process your request right now. Sorry, that\'s all we know.');

            $this->getDataPersistor()->set('contact_us', $post);
            //$this->_redirect('contact/index');
            echo $message;
            return;
        }
    }

    private function getDataPersistor()
    {
        if ($this->dataPersistor === null) {
            $this->dataPersistor = ObjectManager::getInstance()
                ->get(DataPersistorInterface::class);
        }

        return $this->dataPersistor;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top