0

I would like to change the variable - $validate to false from before the plugin. May I know how to do it?

app/code/MyModule/ReviewCheck/Plugin/ReviewModel.php

<?php

namespace MyModule\ReviewCheck\Plugin;

use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;

class ReviewModel {

protected $messageManager;

protected $request;

public function __construct(
    MessageManagerInterface  $messageManager,
    \Magento\Framework\App\Request\Http $request
)
{
    $this->messageManager=$messageManager;
    $this->request=$request;

}

public function beforeExecute(\Magento\Review\Controller\Product\Post $subject) {

    $nickNameChecker = $this->request->getPostValue("nickname");


    if(strpos($nickNameChecker,"-")){

        $this->messageManager->addErrorMessage('Nickname cant has dash'.date('Y-m-d H:i:s'));

    }
    else {
        return $this;
    }

   }
}

\Magento\Review\Controller\Product\Post:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
 namespace Magento\Review\Controller\Product;

 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
 use Magento\Review\Controller\Product as ProductController;
 use Magento\Framework\Controller\ResultFactory;
 use Magento\Review\Model\Review;

 /**
  * Class Post
  */
 class Post extends ProductController implements HttpPostActionInterface
 {
/**
 * Submit new review action
 *
 * @return \Magento\Framework\Controller\Result\Redirect
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function execute()
{
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    if (!$this->formKeyValidator->validate($this->getRequest())) {
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }

    $data = $this->reviewSession->getFormData(true);
    if ($data) {
        $rating = [];
        if (isset($data['ratings']) && is_array($data['ratings'])) {
            $rating = $data['ratings'];
        }
    } else {
        $data = $this->getRequest()->getPostValue();
        $rating = $this->getRequest()->getParam('ratings', []);
    }
    if (($product = $this->initProduct()) && !empty($data)) {
        /** @var \Magento\Review\Model\Review $review */
        $review = $this->reviewFactory->create()->setData($data);
        $review->unsetData('review_id');

        $validate = $review->validate();
        if ($validate === true) {
            ....
}
}

1 Answer 1

0

Due to $validate in execute method in Magento\Review\Controller\Product\Post class being the result of validate method in Magento\Review\Model\Review, you should modify validate method in Magento\Review\Model\Review instead.

You also need to use after plugin instead of before plugin for that method.

So, declare the plugin in app/code/YourVendor/YourModule/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Review\Model\Review">
        <plugin name="AddCustomValidation" type="YourVendor\YourModule\Plugin\AddCustomValidation"/>
    </type>
</config>

Create app/code/YourVendor/YourModule/Plugin/AddCustomValidation.php file:

<?php

namespace YourVendor\YourModule\Plugin;

class AddCustomValidation
{
    public function afterValidate(\Magento\Review\Model\Review $subject, $result)
    {
        $nickName = $subject->getNickname() ?? '';
        if (is_array($result) && $result && strpos($nickName, '-')) {
            $result[] = __('Nickname cant has dash.') . ' ' . date('Y-m-d H:i:s');
        }

        if ($result === true && strpos($nickName, '-')) {
            $result = __('Nickname cant has dash.') . ' ' . date('Y-m-d H:i:s');
        }

        return $result;
    }
}

Remove generated directory and clear cache to get it done.

2
  • Hi, I have changed the code as per your advice but showed error: TypeError: Argument 1 passed to MyModule\ReviewCheck\Plugin\ReviewModel::afterValidate() must be an instance of MyModule\ReviewCheck\Plugin\Magento\Review\Model\Review,
    – hill2011
    Commented Dec 11, 2022 at 13:03
  • It's a small typo, replace Magento\Review\Model\Review with \Magento\Review\Model\Review in app/code/YourVendor/YourModule/Plugin/AddCustomValidation.php. Remove generated directory and clear cache.
    – Tu Van
    Commented Dec 11, 2022 at 13:12

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.