1

I'm trying to extend Magento\Catalog\Block\Product\View to add some functionalities. I have let PHPStorm generate the constructor method for me and that worked fine.

public function __construct(
    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,
    $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    array $data
){
    parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data);
}

But as soon as I add a custom class factory to the constructor it breaks and I get 404 page when I try to visit the product view page. I used the same factory in Adminhtml blocks and observers, so I figured this should work the same way.

protected $_awesomeModelFactory;

public function __construct(
    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,
    $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    array $data,
    \MyAwesome\Mobule\Model\AwesomeModelFactory $awesomeModelFactory
){
    $this->_awesomeModelFactory = $awesomeModelFactory;
    parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data);
}

1 Answer 1

1

The problem here is the order of arguments given to your custom block.

$data should always come last.

On top of that, you forgot to specify a class for the $productRepository parameter in the constructor.

Thus you need to update your constructor with the following code:

public function __construct(
    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,
    ProductRepositoryInterface $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    \MyAwesome\Mobule\Model\AwesomeModelFactory $awesomeModelFactory,
    array $data = []
){
    $this->_awesomeModelFactory = $awesomeModelFactory;
    parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data);
}

Natively in Magento, the Magento\Review\Block\Product\View is a good working example on how to extend the Magento\Catalog\Block\Product\View if you want to have a look at this class.

1
  • Thanks for your reaction. Of course you are right about the order of the arguments in the constructor. That didn't solve it for me. Turns out I have been wasting my time for the past hour..... I just had to run php bin/magento setup:di:compile again. I keep forgetting all the new CLI options and that only cleaning the cache won't do the trick.
    – bleenders
    May 31, 2016 at 12:32

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.