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">
&lt;type name=&quot;Magento\Review\Block\Adminhtml\Edit\Form&quot;&gt;
    &lt;plugin name=&quot;modify_review_edit_form&quot; type=&quot;Vendor\NewField\Plugin\Edit\Form&quot; /&gt;
&lt;/type&gt;    

</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">
&lt;event name='review_save_after'&gt;
    &lt;observer name='newfield_save_after' instance='Vendor\NewField\Model\Observer\ReviewSave'/&gt;
&lt;/event&gt;    

</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-&gt;startSetup();

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

    $setup-&gt;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-&gt;getEvent()-&gt;getDataObject();
    $connection = $this-&gt;_resource;

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

    if (empty($review-&gt;getReplay())) return;

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

    if ($detailId) {
        $condition = [&quot;detail_id = ?&quot; =&gt; $detailId];
        $connection-&gt;getConnection()-&gt;update($tableName, $detail, $condition);
    } else {
        $detail['store_id'] = $review-&gt;getStoreId();
        $detail['customer_id'] = $review-&gt;getCustomerId();
        $detail['review_id'] = $review-&gt;getId();
        $connection-&gt;getConnection()-&gt;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-&gt;_coreRegistry-&gt;registry('review_data');

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

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

    $form-&gt;setValues($review-&gt;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?

Jack Brooks
  • 319
  • 4
  • 15

0 Answers0