2

I'm trying to create a custom widget by extending the existing "Banner Rotator" widget. Something similar to this.

I placed the widget.xml in my module and created a class extending \Magento\Banner\Block\Widget\Banner. Then my custom widget started to appear in BO and works fine (works just like the Magento widget because I haven't customized it yet).

Now I intend to do some additional manipulations to the rotator using the applied layered navigation filters. To do that I'm trying to inject the \Magento\Catalog\Model\Layer\Resolver in to my class. But it fails with this error message.

Fatal error: Uncaught TypeError: Argument 4 passed to MyNamespace\Module\Block\Widget\Banner::__construct() must be an instance of Magento\Catalog\Model\Layer\Resolver, none given, called in /var/www/html/var/generation/MyNamespace/Module/Block/Widget/Banner/Interceptor.php on line 14 and defined in /var/www/html/app/code/MyNamespace/Module/Block/Widget/Banner.php on line 12

Looks like I cannot have a fourth argument in my class.

My class looks like below.

namespace MyNamespace\Module\Block\Widget;
class Banner extends \Magento\Banner\Block\Widget\Banner
{
protected $layerResolver;
public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Banner\Model\ResourceModel\Banner $resource,
 array $data = [],
 \Magento\Catalog\Model\Layer\Resolver $resolver
)
{
 parent::__construct(
 $context,
 $resource,
 []
 );
 $this->layerResolver = $resolver;
}
}

How do I add the 4th argument?

Rama Chandran M
3,26515 gold badges24 silver badges38 bronze badges
asked Jul 1, 2017 at 17:51
0

4 Answers 4

3

You just need to delete var/generation folder because every time when you construct new class in __construct() function. Magento 2 create Interceptor.php with constucted class.

For example you have controller at

app/code/Vendor/Module/Controller/MyController.php

For this controller Magento 2 create Interceptor.php at

var/generation/Vendor/Module/Controller/MyController/Interceptor.php

So when you refresh second time Magento 2 look for construct class in Interceptor.php. So when you construct new class in controller you need to delete old constructed class in var/generation folder manually or by this command:

sudo rm -rf var/generation/*

answered Jul 2, 2017 at 9:27
1

Put $data as last variable in the constructor. Any scalar variables should be last.

answered Jul 1, 2017 at 23:49
1
  • This was also a mistake I made but not causing the error I got. Thanks for pointing it out Commented Jul 2, 2017 at 5:45
0

Run php bin/magento setup:upgrade from your magento root folder

Make sure you are in developer mode

answered Jul 1, 2017 at 19:32
0

Fixed it. Cleaning auto generated files manually fixed the issue. Basically I had to regenerate the interceptor for the class.

answered Jul 2, 2017 at 5:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.