I need help regarding block overriding. I have successfully overridden a block using preferences, but when I add core registry in the construct method it's showing the following error:

Recoverable Error: Argument 2 passed to Magento\Catalog\Block\Product\ProductList\Toolbar::__construct() must be an instance of Magento\Catalog\Model\Session, array 

This is my code:

namespace Vendername\Modulename\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar

    protected $_registry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        Registry $registry,
        array $data = []
    ) {

        $this->_registry = $registry;
        parent::__construct($context, $data);

    public function getCurrentMode()

        $category = $this->_registry->registry('current_category');
        $mode = $this->_getData('_current_grid_mode');
        if ($mode) {
            return $mode;
        $defaultMode = $this->_productListHelper->getDefaultViewMode($this->getModes());
        $mode = $this->_toolbarModel->getMode();
        if (!$mode || !isset($this->_availableMode[$mode])) {
            $mode = $defaultMode;
        $this->setData('_current_grid_mode', $mode);
        return $mode;

If I remove registry and this line, override is working.

1 Answer 1


Looks like problem that Toolbar block has many dependencies, but you dont pass their

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 = []

But use "preferences" to overwrite classes it is bad idea at all. Better use plugins or layout depends on what you need.

  • Hi Thank You for answering if i add all dependency its showing following error "Invalid block type: Magento\Catalog\Block\Product\ProductList\Toolbar #0 E:\wamp\www\magento2\lib\internal\Magento\Framework\View\Layout\Generator\Block.php(237)" .. <br/> You have any tutorial for override toolbar blocl using plugin ya any reference to help me .
    – Pratik
    Oct 14, 2015 at 12:55
  • gist.github.com/kandy/ce19743a7cdf5b3b82a4 class from my gist works properly
    – KAndy
    Oct 14, 2015 at 13:22

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.