سؤال

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!

هل كانت مفيدة؟

المحلول

Check all of your namespaces, class names, file & folder names. This error can occurs when your namespace is like:

Vendor\Module\Block\SomeName\AnotherName

and your path to that file is like:

Vendor/Module/Block/Somename/Anothername

The second word starts from lowercase, in this situation, all can work fine before the compilation and doesn't work at all after, because the PHP array-keys is a case-sensitive but namespaces is not case-sensitive.

Here is similar problem with a controller Uncaught TypeError: Argument 1 passed to [...] must be an instance of

نصائح أخرى

Try the following code, this worked for me on many occasions for creating widgets.

public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->setTemplate('widget/custom-template.phtml');
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top