0

I am trying to use a plugin to alter the contact form post data by using the afterExecute method:

    public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result)
    {
        $comment =  $subject->getRequest()->getParam('comment', false);
        $subject->getRequest()->setParam('comment', 'testing: '.$comment); // <--- this is not working and not changing the comment post value
        return $result;
    }

I just want to append/prepend a string to the comment (message) that is posted. But the above does not work and the comment post value is not changed. Thanks

2 Answers 2

3

use before plugin instead of after plugin.

public function beforeExecute(\Magento\Contact\Controller\Index\Post $subject)
{
    $comment =  $subject->getRequest()->getParam('comment', false);
    $subject->getRequest()->setParam('comment', 'testing: '.$comment); 
}

try this.

6
  • Thanks. I get this error though: Error: Cannot use object of type Magento\Framework\Controller\Result\Redirect\Interceptor as arra
    – sulman
    Apr 7, 2021 at 14:07
  • Can you just debug and show here what you are getting in $result ? Apr 7, 2021 at 14:26
  • i have checked actual function and it doesn't have $result return with comment, so you just have to use before plugin instead of after plugin Apr 7, 2021 at 14:28
  • @sulman answer updated. Apr 7, 2021 at 14:31
  • Perfect! Works well. Thank you :)
    – sulman
    Apr 7, 2021 at 14:42
1

Try before plugin with below code

<?php
declare(strict_types=1);

namespace Vendor\MyModule\Plugin\Frontend\Magento\Contact\Controller\Index;

class Post
{

    public function beforeExecute(
        \Magento\Contact\Controller\Index\Post $subject
    ) {
        $comment =  $subject->getRequest()->getParam('comment', false);
        $subject->getRequest()->setParam('comment', 'testing: '.$comment);
        return;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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