Error while overriding block via preference in Magento 2
While overriding block via preference, Magento 2 can show an error when you add a core registry in a construct method.
If the error looks as follows:
1 |
Recoverable Error: Argument 2 passed to Magento\Catalog\Block\Product\ProductList\Toolbar::__construct() must be an instance of Magento\Catalog\Model\Session, array |
you can easily fix the problem.
Probably, Toolbar block has many dependencies, but at the same time you don’t pass them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\Session $catalogSession, \Magento\Catalog\Model\Config $catalogConfig, ToolbarModel $toolbarModel, \Magento\Framework\Url\EncoderInterface $urlEncoder, ProductList $productListHelper, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, array $data = [] ) |
Do not use preferences to overwrite classes. It’s better to rely on plugins or layout depends.
Source (
More Magento 2 tips from the Cookbook