0

I am trying to rewrite the \Magento\Catalog\Model\Layer\Filter\Item class in Magento by adding the following line to my di.xml:

<preference for="Magento\Catalog\Model\Layer\Filter\Item"
            type="Vendor\Module\Rewrite\Magento\Catalog\Model\Layer\Filter\Item"/>

Magento takes my Vendor\Module-class over it's default class, but I need to add some dependencies to it. Now I tried to add them by adjusting the __construct()-method and adding them there, but this doesn't work (which makes sence of course, because it breaks the chain for classes that try to extend the Item-object itself:

public function __construct(
    \Vendor\Module\Helper\Data $dataHelper,
    \Magento\Framework\UrlInterface $url,
    \Magento\Theme\Block\Html\Pager $htmlPagerBlock,
    array $data
) {
    $this->dataHelper = $dataHelper;
    parent::__construct($url, $htmlPagerBlock, $data);
}

The error I get is the following:

Missing required argument $data of Vendor\Module\Rewrite\Magento\Catalog\Model\Layer\Filter\Item.

So what's the proper way to get my helper in my rewritten object?

1
  • What if you move your \Vendor\Module\Helper\Data $dataHelper in the end of your parameters list after the array $data? Mar 21, 2016 at 17:15

3 Answers 3

3

What you have done is correct but you have made the final $data argument required by not adding = [] after it as a default value.

Simply change array $data to array $data = [].

You may also have to clear the var/generation folder because you have changed the constructor params.

1
  • Thanks! I tried that, but I didn't clear my var/generation folder. Works like a charm now! Mar 22, 2016 at 8:51
0

Replace your construct() method with below code,

public function __construct(
    \Vendor\Module\Helper\Data $dataHelper,
    \Magento\Framework\UrlInterface $url,
    \Magento\Theme\Block\Html\Pager $htmlPagerBlock,
    array $data = []
) {
    $this->dataHelper = $dataHelper;
    parent::__construct($url, $htmlPagerBlock, $data);
}

Remove var/generation folder and reload page.

0

Exception #0 (BadMethodCallException): Missing required argument $data when the order of the constructor parameters was wrong.

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,    
    \Magento\Framework\Url\Helper\Data $frameworkHelperDataHelper,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,  
    array $data = [],        
    \Magento\Framework\DataObjectFactory $dataObjectFactory,
    \Magento\Framework\App\Filesystem\DirectoryList $directoryList
)

I was supposed to pass array $data = [], in the last

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,    
    \Magento\Framework\Url\Helper\Data $frameworkHelperDataHelper,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\DataObjectFactory $dataObjectFactory,
    \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
    array $data = []  
)

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.