1

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;
    }
}       
1
  • which version of magento are you using? Commented Jun 3, 2018 at 7:25

1 Answer 1

4

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;
    }
}
13
  • Where i need to add ? in constructor ?
    – Kanika
    Commented Jun 3, 2018 at 7:20
  • yes, just copy \Magento\Contact\Controller\Index\Post constructor method 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
    – kunj
    Commented Jun 3, 2018 at 7:23
  • Maybe you will need di compile and cache flush after this changes.
    – kunj
    Commented Jun 3, 2018 at 7:28
  • let me try this
    – Kanika
    Commented Jun 3, 2018 at 7:32
  • codeshare.io/5zbb6O please review this code, i am getting different error now
    – Kanika
    Commented Jun 3, 2018 at 7:38

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.