cancel
Showing results for 
Search instead for 
Did you mean: 

magento 2 override block using prefrence.

SOLVED

magento 2 override block using prefrence.

 Hello Community forum,

i need small help regarding override block

i done succesfully override block using prefrence but when i add core registry in constuct method its showing 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 given 

 

 This is my code 

<?php
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');
print_r($category->getId()); exit;
$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 

$category= $this->_registry->registry('current_category');
print_r($category->getId()); exit;

 

override  is working.

 

Thank You

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: magento 2 override block using prefrence.

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)

 

i found solution on stack exchange

 

http://magento.stackexchange.com/questions/86435/magento-2-override-block-using-preference

 

Thank You

View solution in original post

5 REPLIES 5

Re: magento 2 override block using prefrence.

parent::__construct($context, $data); used to corrospond to \Magento\Framework\View\Element\Template however now in your preference block it is \Magento\Catalog\Block\Product\ProductList which if you look at that block you will see it required more params the second of which should be \Magento\Catalog\Model\Session as per the error message.

If your going to extend the original you need to pass in the correct params to the parent call.

James Anelay - TheExtensionLab - Simple and efficient Magento extensions that do one thing and do it well. http://www.theextensionlab.com/

Re: magento 2 override block using prefrence.

Hi @khamarpratik,

 

Please first of all check the __construct method of parent class (in this case \Magento\Catalog\Block\Product\ProductList\Toolbar) that you have extended from your class . You need to pass the same parameters of parent class construct method while calling parent::__construct method from child class.

Your custom class __construct method should be like this.

 

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,
        \Magento\Catalog\Helper\Product\ProductList $productListHelper,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,

        Registry $registry,
        array $data = []
    ) {
        $this->_catalogSession = $catalogSession;
        $this->_catalogConfig = $catalogConfig;
        $this->_toolbarModel = $toolbarModel;
        $this->urlEncoder = $urlEncoder;
        $this->_productListHelper = $productListHelper;
        $this->_postDataHelper = $postDataHelper;

        $this->_registry=$registry;
        parent::__construct($context, $catalogSession, $catalogConfig, $toolbarModel, $urlEncoder, $productListHelper, $postDataHelper);
    }

-----
Darshan, Solution Consultant
Problem solved? Click Accept as Solution!

Re: magento 2 override block using prefrence.

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)

 

i found solution on stack exchange

 

http://magento.stackexchange.com/questions/86435/magento-2-override-block-using-preference

 

Thank You

Re: magento 2 override block using prefrence.

Hi Darshan

 

I'm trying to install magento 2 on my linux server through the cpanel and using the default setup procedure. My server meets all the needed requirements. But in the standard 7-step instal, the minute i reach at step 6 and the installation is at 67% percent, it just hangs up. I tried doing both and also atleast 3 separate instances of each (with and without data sample installation) and it seems to be stopping at the exact point of 67% specifically when its at the installation of module_theme. 

 

Also the option to 're-try' or 'next' all seems greyed out so unable to force click.

 

Any idea why and has anyone else been experiencing this !

 

Thanks Ketan

 

Re: magento 2 override block using prefrence.

Hi @ke007,

 

Try to add below line in .htaccess file to resolve this issue:
php_value xdebug.max_nesting_level 500

 

If still problem persists check in database tables at 67%. Is there any updates going on. Sample data installation is very slow process in some system so wait for a long time.

 

-----
Darshan, Solution Consultant
Problem solved? Click Accept as Solution!