I'm a new Magento 2 developer that's trying to create a widget with a picture in it and I've been following some tutorials to get started. I'm having some trouble though with dependency injection in the Block-class.
Here is the error:
Error filtering template: Recoverable Error: Argument 1 passed to Magento\Framework\View\Element\Template::__construct() must be an instance of Magento\Framework\View\Element\Template\Context, instance of Magento\Framework\ObjectManager\ObjectManager given, called in C:\xampp\htdocs\magento\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 93 and defined in C:\xampp\htdocs\magento\vendor\magento\framework\View\Element\Template.php on line 128
The error obviously states that the constructor of the parent if acquiring the wrong information, but as far as I can tell I'm feeding it the correct data.
Here is my class:
<?php
namespace X\Y\Block\Widget;
class CustomWidget extends \Magento\Framework\View\Element\Template
implements \Magento\Widget\Block\BlockInterface
{
public function _toHtml()
{
$this->setTemplate('widget/custom_widget.phtml');
}
}
I've heard across the web though that using the _toHtml()
function is bad practice and I should use the constructor instead, but that gives me the same effect. So far I've used these constructors:
protected function _construct()
{
parent::_construct();
$this->setTemplate('widget/custom_widget.phtml');
}
public function _construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::_construct($context, $data);
$this->setTemplate('widget/custom_widget.phtml');
}
I have also tried just setting
protected $_template = "widget/custom_widget.phtml";
but that generates the same result.
Have anyone seen this before? I have reinstalled Magento several times and my co-workers do not have this problem, so there's definitely something weird going on here.
Any help is much appreciated!
public function _construct
method name is typo or you have it in the code?\Magento\Framework\Model\AbstractModel::__construct()
,\Magento\Framework\View\Element\AbstractBlock::__construct()
and maybe somewhere else), but it has no arguments (not compatible with dependency injection).