4

I'm working on Custom module in which I need to override Magento's reviews Module. I want to add some extra fields in Magento's Review Form. I created Preferences in adminhtml/di.xml Now whenever I add my custom fields code, all the other fields get removed.

class Form extends \Magento\Backend\Block\Widget\Form\Generic
{

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
    \Magento\Review\Helper\Data $reviewData,
    array $data = []
) {
    $this->_reviewData = $reviewData;
    $this->_systemStore = $systemStore;
    parent::__construct($context, $registry, $formFactory, $data);
}

/**
 * Prepare add review form
 *
 * @return void
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
protected function _prepareForm()
{
    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create();

    $fieldset = $form->addFieldset('add_review_form', ['legend' => __('Review Details')]);

    $fieldset->addField('product_name', 'note', ['label' => __('Product'), 'text' => 'product_name']);

    // $fieldset->addField(
    //     'detailed-rating',
    //     'note',
    //     [
    //         'label' => __('Product Rating'),
    //         'required' => true,
    //         'text' => '<div id="rating_detail">' . $this->getLayout()->createBlock(
    //             \Magento\Review\Block\Adminhtml\Rating\Detailed::class
    //         )->toHtml() . '</div>'
    //     ]
    // );

    // $fieldset->addField(
    //     'status_id',
    //     'select',
    //     [
    //         'label' => __('Status'),
    //         'required' => true,
    //         'name' => 'status_id',
    //         'values' => $this->_reviewData->getReviewStatusesOptionArray()
    //     ]
    // );

    // /**
    //  * Check is single store mode
    //  */
    // if (!$this->_storeManager->isSingleStoreMode()) {
    //     $field = $fieldset->addField(
    //         'select_stores',
    //         'multiselect',
    //         [
    //             'label' => __('Visibility'),
    //             'required' => true,
    //             'name' => 'select_stores[]',
    //             'values' => $this->_systemStore->getStoreValuesForForm()
    //         ]
    //     );
    //     $renderer = $this->getLayout()->createBlock(
    //         \Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element::class
    //     );
    //     $field->setRenderer($renderer);
    // }

    // $fieldset->addField(
    //     'nickname',
    //     'text',
    //     [
    //         'name' => 'nickname',
    //         'title' => __('Nickname'),
    //         'label' => __('Nickname'),
    //         'maxlength' => '50',
    //         'required' => true
    //     ]
    // );

    // $fieldset->addField(
    //     'title',
    //     'text',
    //     [
    //         'name' => 'title',
    //         'title' => __('Summary of Review'),
    //         'label' => __('Summary of Review'),
    //         'maxlength' => '255',
    //         'required' => true
    //     ]
    // );

    // $fieldset->addField(
    //     'detail',
    //     'textarea',
    //     [
    //         'name' => 'detail',
    //         'title' => __('Review'),
    //         'label' => __('Review'),
    //         'required' => true
    //     ]
    // );

     $fieldset->addFieldset(
        'review_comment',
        ['legend' => __('Review Comment'), 'class' => 'fieldset-wide']
    );

    $fieldset->addField(
        'title_comment',
        'text',
        ['label' => __('Title'), 'required' => false, 'name' => 'comment-title']
    );
    $fieldset->addField(
        'comment',
        'textarea',
        ['label' => __('Comment'), 'required' => false, 'name' => 'comment']
    );
    $fieldset->addField(
        'send_customer',
        'checkbox',
        ['label' => __('Send this Comment to customer'), 'required' => false, 'name' => 'show_to_customer']
    );


    $fieldset->addField('product_id', 'hidden', ['name' => 'product_id']);

    /*$gridFieldset = $form->addFieldset('add_review_grid', array('legend' => __('Please select a product')));
      $gridFieldset->addField('products_grid', 'note', array(
      'text' => $this->getLayout()->createBlock(\Magento\Review\Block\Adminhtml\Product\Grid::class)->toHtml(),
      ));*/

    $form->setMethod('post');
    $form->setUseContainer(true);
    $form->setId('edit_form');
    $form->setAction($this->getUrl('review/product/post'));

    $this->setForm($form);
    return parent::_prepareForm();
    }
}

also, whenever I add my fields using this method $fieldset->addField it gives an error.

3 Answers 3

4

Use plugin for that.

<?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\Add\Form">
       <plugin name="review_add_form_custom" type="QaisarSatti\HelloWorld\Plugin\Add\Form" />
   </type>
</config>

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_cusom',
           ['legend' => __('Review Custom Field'), 'class' => 'fieldset-wide']
       );

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


       return [$form];
   }
}

Reference

1
  • Our pleasure. Happy codding!
    – PROGOSTECH
    Commented Aug 10, 2018 at 17:58
0

If You need to override Magento's reviews Module. I want to add some extra fields in Magento's Review Form.

MagentoFolder/vendor/magento/module-review/view/frontend/templates/form.phtml

Override the Following in your custom Module Layout File.

https://prnt.sc/kffwcc

1
  • This frontend form. I want to override backend form.
    – sudo55
    Commented Aug 8, 2018 at 13:10
0
   *$fieldset->addFieldset(*
      'review_comment',
       ['legend' => __('Review Comment'), 'class' => 'fieldset-wide']
   );

fieldset doesn't have addFieldset() method.So change above code snippet to:

   $fieldset->addField(
      'review_comment',
       ['legend' => __('Review Comment'), 'class' => 'fieldset-wide']
   );
2
  • I am still confused. Can you plz add one field using above snippet?
    – sudo55
    Commented Aug 8, 2018 at 13:12
  • If you take a look at your posted source code, you will find you are doing like: $fieldset->addFieldset(...). I think this is incorrect.
    – Jeff Yu
    Commented Aug 10, 2018 at 0:16

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.