2

I have created the following:

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_NewField',
    __DIR__
);

etc\module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_NewField" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Review" />
        </sequence>
    </module>
</config>

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\Block\Adminhtml\Edit\Form">
        <plugin name="modify_review_edit_form" type="Vendor\NewField\Plugin\Edit\Form" />
    </type>    

</config>

events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name='review_save_after'>
        <observer name='newfield_save_after' instance='Vendor\NewField\Model\Observer\ReviewSave'/>
    </event>    

</config>

Setup\InstallSchema.php

<?php
namespace Vendor\NewField\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $installer->getConnection()->addColumn(
            $installer->getTable('review_detail'),
            'replay',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true,
                'comment' => 'Replay'
            ]
        );

        $setup->endSetup();

    }
}

Observer\ReviewSave.php

<?php

namespace Vendor\NewField\Model\Observer;

use Magento\Framework\Event\ObserverInterface;

class ReviewSave implements ObserverInterface
{
    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    ) {
        $this->_resource = $resource;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $review = $observer->getEvent()->getDataObject();
        $connection = $this->_resource;

        $tableName = $connection->getTableName('review_detail');
        $detail = [
            'replay' => $review->getReplay(),
        ];

        if (empty($review->getReplay())) return;

        $select = $connection->getConnection()->select()->from($tableName)->where('review_id = :review_id');
        $detailId = $connection->getConnection()->fetchOne($select, [':review_id' => $review->getId()]);

        if ($detailId) {
            $condition = ["detail_id = ?" => $detailId];
            $connection->getConnection()->update($tableName, $detail, $condition);
        } else {
            $detail['store_id'] = $review->getStoreId();
            $detail['customer_id'] = $review->getCustomerId();
            $detail['review_id'] = $review->getId();
            $connection->getConnection()->insert($tableName, $detail);
        }   
    }
}

Plugin -> Edit\Form.php

<?php

namespace Vendor\NewField\Plugin\Edit;

class Form extends \Magento\Review\Block\Adminhtml\Edit\Form
{
    public function beforeSetForm(\Magento\Review\Block\Adminhtml\Edit\Form $object, $form) {

        $review = $object->_coreRegistry->registry('review_data');

        $fieldset = $form->addFieldset(
            'replay_text',
            ['legend' => __('Review Replay'), 'class' => 'fieldset-wide']
        );

        $fieldset->addField(
            'replay',
            'text',
            ['label' => __('Replay'), 'required' => false, 'name' => 'replay']
        );

        $form->setValues($review->getData());

        return [$form];
    }
}

view\adminhtml\ui_component\review_listing.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="review_columns">
        <column name="replay">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Replay</item>
                    <item name="sortOrder" xsi:type="number">45</item>
                    <item name="truncate" xsi:type="number">50</item>
                    <item name="nl2br" xsi:type="boolean">true</item>
                    <item name="escape" xsi:type="boolean">true</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

This works perfectly In admin area, I can add a reply however i want and it saves to the Database. I am not able to show the reply in frontend. I don't know which file i need to change. I tried to change this file Magento_Review/templates/product/view/list.phtml I couldn't even override this from my custom module.

Please can someone help me and tell me where i am doing it wrong. I have used these links as guide : Add email field to Review Form - Magento 2 How to override product review list.phtml file?

0

Your Answer

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