1

I extend Magento\Review\Block\Product\View\ListView with my own custom class, called Review.

As learned I need to extend the constructor with the same data that gets used in Magento\Review\Block\Product\View, which is extended by ListView.

However, as soon as I bring a constructor into my code, it fails. The question is: why?

Review.php (Custom Extend):

<?php

namespace MS\RealReviews\Block;

/**
 * Entity rating block
 *
 */

class Review extends \Magento\Review\Block\Product\View\ListView
{

    protected $customer_id;
    protected $_orderCollectionFactory;

    public function __construct(    
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        array $data = []
    ) {
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct(
             $context, 
             $urlEncoder,
             $jsonEncoder,
             $string,
             $productHelper,
             $productTypeConfig,
             $localeFormat,
             $customerSession,
             $productRepository,
             $priceCurrency,
             $collectionFactory,
             $data
        );
    }

    protected function _setCustomerID($input){
        $this->customer_id = $input;
    }

    protected function _getCustomerID(){
        return $this->customer_id;
    }

    public function isRealReview($customer_id){

        $html = '';
        $product = $this->getProductId();
        $this->_setCustomerID($customer_id);

        $html .= $this->_getCustomerID();


        return $html;
    }

}

If I delete the constructor from the code, it works again. How can I fix that?

  • Did you try to clear the var/generation folder after adding the constructor ? – Raphael at Digital Pianism Mar 30 '17 at 8:29
  • I'm at developer mode, so there isn't really a need for that AFAIK – Max Mar 30 '17 at 8:44
  • you still need to clear that var/generation folder manually even in dev mode as soon as you modify the constructors of classes – Raphael at Digital Pianism Mar 30 '17 at 8:53
  • Cool. Actually that seemed to work. Thanks, Raphael. If you write an answer with that and maybe a little more detail I'm happy to mark it as the correct answer to help others in the future! – Max Mar 30 '17 at 8:58
1

The solution is to clear the var/generation code.

This is indeed required as soon as you start dealing with class constructors, you can find out more here in the official documentation: http://devdocs.magento.com/guides/v2.0/howdoi/php/php_clear-dirs.html

  • Using $ rm -rf var/generation/* has worked perfectly fine. Magento2 could handle this a little better IMHO, but its fine. Thanks for the hint, Raphael. – Max Mar 30 '17 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.