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?
4 Answers 4
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/*
Put $data as last variable in the constructor. Any scalar variables should be last.
-
This was also a mistake I made but not causing the error I got. Thanks for pointing it outShanR– ShanR2017年07月02日 05:45:51 +00:00Commented Jul 2, 2017 at 5:45
Run php bin/magento setup:upgrade from your magento root folder
Make sure you are in developer mode
Fixed it. Cleaning auto generated files manually fixed the issue. Basically I had to regenerate the interceptor for the class.