Magento – Magento 2 How to add Product Review Form to Popup

frontendfrontend-modelmagento-2.1magento2model

I want to add a Popup for product review form.

I was able to create pop up with js:

define(
    [
        'jquery',
        'Magento_Ui/js/modal/modal'
    ],
    function ($) {
        "use strict";
        //creating jquery widget
        $.widget('vendor.modalForm', {
            options: {
                modalForm: '#modal-form',
                modalButton: '.open-modal-form'
            },
            _create: function () {
                this.options.modalOption = this._getModalOptions();
                this._bind();
            },
            _getModalOptions: function () {
                /**
                 * Modal options
                 */
                var options = {
                    type: 'popup',
                    responsive: true,
                    title: '',
                };

                return options;
            },
            _bind: function () {
                var modalOption = this.options.modalOption;
                var modalForm = this.options.modalForm;

                $(document).on('click', this.options.modalButton, function () {
                    //Initialize modal
                    $(modalForm).modal(modalOption);
                    //open modal
                    $(modalForm).trigger('openModal');
                });
            }
        });

        return $.vendor.modalForm;
    }
);

I need to make it work to were when people click link it shows the review form:

Here is my html for the .phtml file:

<div style="display:none;" id="modal-form">
    <?php include ($block->getTemplateFile('Magento_Review::form.phtml')) ?>
    // here should go the code for the form
</div>
<a class="action open-modal-form" href="#" title="Modal">
    <span>Review Product</span>
</a>
<script type="text/x-magento-init">
    {
        ".open-modal-form": {
            "Vendor_Module/js/modal-form": {}
        }
    }
</script>

Any Suggestions to make this work?

I added the code: <?php include ($block->getTemplateFile('Magento_Review::form.phtml')) ?> but it doesn't bring the product form it shows as if I had to register or login to create the review. Is there a way to pass the product id variable? I have that on my .phtml

Best Answer

The problem here is that the Block class Form.php (Magento\Review\Block\Form) is not accessible as you have just included the PHTML file without its class.

You need to change below mentioned line of code:

<?php include ($block->getTemplateFile('Magento_Review::form.phtml')) ?>

to

<?php echo $this->getLayout()->createBlock("Magento\Review\Block\Form")->setTemplate("Magento_Review::form.phtml")->toHtml(); ?>
Related Topic