1

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!

4
  • The one underscore in the public function _construct method name is typo or you have it in the code? Commented Mar 31, 2017 at 16:15
  • Well, right now it is in my code yes, but I've also tried __construct with two underscores and I've seen different sites say different things. Which is correct? Commented Apr 7, 2017 at 11:32
  • You should use two underscores in real construct method. The one-underscore construct are called at the end of real construct method in the abstract block & model classes (\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). Commented Apr 7, 2017 at 11:38
  • Oh wow, I had no idea. Thanks a bunch! I've been working on different things this week but I'll check all the answers here now and see if it solves my problem. Commented Apr 7, 2017 at 11:50

2 Answers 2

1

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

1
  • @OscarRomin I was glad to help you! :) Commented Apr 7, 2017 at 13:29
0

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');
    }

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.