Question

I want to add custom column to admin grid on Marketing → All Review page (Magento_Review module). I create module

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0" encoding="utf-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Review"/>
        </sequence>
    </module>
</config>

app/code/Vendor/Module/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" class="Magento\Ui\Component\Listing\Columns">
        <column name="review_child" class="Vendor\Module\Ui\Component\Listing\Column\MyColumn">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="boolean">false</item>
                    <item name="label" xsi:type="string" translate="true">My Column</item>
                    <item name="sortOrder" xsi:type="number">95</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

app/code/Vendor/Module/registration.php

<?php

declare(strict_types=1);

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);

app/code/Vendor/Module/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">

    <preference for="Vendor\Module\Block\Adminhtml\ReviewGrid" type="Magento\Review\Block\Adminhtml\Grid" />

</config>

app/code/Vendor/Module/Block/Adminhtml/ReviewGrid.php

<?php

namespace Vendor\Module\Block\Adminhtml;

class ReviewGrid extends \Magento\Review\Block\Adminhtml\Grid
{
    protected function _prepareColumns()
    {
        parent::_prepareColumns();
        $this->addColumn(
            'review_child',
            [
                'header' => __('review child'),
                'type' => 'text',
                'index' => 'review_child',
                'escape' => true
            ]
        );
        return $this;
    }
}

app/code/Vendor/Module/Ui/Component/Listing/Column/MyColumn.php

<?php

namespace Vendor\Module\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class MyColumn extends Column
{
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $item[$this->getData('name')] = "MY DATA";
            }
        }
        return $dataSource;
    }
}

Execute the command bin/magento setup:upgrade but I don`t see the column. How can I add my custom column to Magento_Review module?

Was it helpful?

Solution

I have create email field in Magento_Review module in front end review form as well as stored value in database and shown in admin as per your requirement.

I have created custom module for that. Same according to my module you can do for your custom field.

you can download whole module from my google drive link - https://drive.google.com/file/d/1UqTENlYtThPuLnOqpSLhuIwfg8RJqqv2/view?usp=sharing
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top