Questions
I want to override Magento’s reviews Module. I need to add some extra fields in Magento’s Review Form, which is on admin side. I created Preferences in di.xml now whenever I add my custom fields code, all the other fields get removed after loading the page. Is there any way to override them?
0 Answers
Use plugin for that.
Review Add Form
Now Create Form.php file in following directory
app\code\QaisarSatti\HelloWorld\Plugin\Adminhtml\Add
namespace QaisarSatti\HelloWorld\Plugin\Adminhtml\Add;
class Form extends \Magento\Review\Block\Adminhtml\Add\Form
{
public function beforeSetForm(\Magento\Review\Block\Adminhtml\Add\Form $object, $form) {
$fieldset = $form->addFieldset(
'review_custom',
['legend' => __('Review Custom Field'), 'class' => 'fieldset-wide']
);
$fieldset->addField(
'test',
'text',
['label' => __('Test'), 'required' => false, 'name' => 'test']
);
return [$form];
}
}
class Form extends \Magento\Review\Block\Adminhtml\Add\Form
{
public function beforeSetForm(\Magento\Review\Block\Adminhtml\Add\Form $object, $form) {
$fieldset = $form->addFieldset(
'review_custom',
['legend' => __('Review Custom Field'), 'class' => 'fieldset-wide']
);
$fieldset->addField(
'test',
'text',
['label' => __('Test'), 'required' => false, 'name' => 'test']
);
return [$form];
}
}
Review Edit Form
Now Create Form.php file in following directory
app\code\QaisarSatti\HelloWorld\Plugin\Adminhtml\Edit
namespace QaisarSatti\HelloWorld\Plugin\Adminhtml\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(
'review_custom',
['legend' => __('Review Custom Field'), 'class' => 'fieldset-wide']
);
$fieldset->addField(
'test',
'text',
['label' => __('Test'), 'required' => false, 'name' =>; 'test']
);
$form->setValues($review->getData());
return [$form];
}
}
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(
'review_custom',
['legend' => __('Review Custom Field'), 'class' => 'fieldset-wide']
);
$fieldset->addField(
'test',
'text',
['label' => __('Test'), 'required' => false, 'name' =>; 'test']
);
$form->setValues($review->getData());
return [$form];
}
}