9

I'm trying to create a block I can call in my override of 'root.phtml'.

In the root file, I added some extra classes to the body depending on the url, so I've created a module which I call on this page.

I've overridden root.phtml in my theme folder, and I've created a seperate module to call in this phtml file. This is just to get the current URL (so if you have a better suggestion, please come forward) however when I try to load the application it comes up with

    Missing required argument $data of Karl\OverrideView\Block\OverrideView

The file being refered to is;

namespace Karl\OverrideView\Block;

class OverrideView
{

public $assetRepository;
protected $_storeManager;

public function __construct(

    \Magento\Framework\View\Element\Template\Context $context,
    array $data = [],
    \Magento\Store\Model\StoreManagerInterface $storeManager
){
    $this->_storeManager = $storeManager;
    return parent::construct($context, $data);
}

public function getFullUrl(){
    return $this->_storeManager->getStore()->getCurrentUrl(false);
}
}

2 Answers 2

16

In your constructor you are calling parent::construct(), but you are not extending any class. You must extend the class to which you are override or to the class \Magento\Framework\View\Element\Template and there is a typo in constructor name (two underscores are missing), the proper name is __construct

Also, write your constructor this way:

public function __construct(

    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
){
    $this->_storeManager = $storeManager;
    return parent::__construct($context, $data);
}
1
  • Realised I'd not given this answer any credit, Thanks! Commented Oct 9, 2017 at 13:24
11

I was having the same error, all my __constructs and Extending's are correct, if you are scratching your head wondering... why when I do:

php bin/magento setup:upgrade

it breaks everything and get this error, but then when I do:

php bin/magento setup:di:compile

the same code works fine afterwards, but then would break again if you do another:

php bin/magento setup:upgrade

??

Then most likely is that you have got your array $DATA in the middle of your Construct definition, this would cause this issue and the solution is to move this right to the end of your __constrct definition, see example below:

Example, this will NOT work:

public function __construct (
      \Magento\Framework\Model\Context $ModelContext, 
      array $Data = [],
      \Magento\Framework\Registry $Registry

But this will work:

public function __construct (
      \Magento\Framework\Model\Context $ModelContext,
      \Magento\Framework\Registry $Registry,
      array $Data = []

I hope this helps.

5
  • 2
    Thank you, fixed my issue (BadMethodCallException). Commented Nov 30, 2020 at 15:08
  • 1
    Glad to hear that Kevin, good luck. Commented Dec 3, 2020 at 16:41
  • 2
    thanks Dude! fixed my issue too (BadMethodCallException) Commented Oct 9, 2021 at 18:35
  • Anyone figured out why the position of $data argument is affecting the code compilation ?
    – sudh_
    Commented Jun 20, 2023 at 19:13
  • I believe its how the system was put together, even if someone figured it out and fixed it, the next next magento-update would likely yo break it again, ... I don't think it matters much "why" this is happening, but if you really like to know it's to do with the empty array [] passed in the middle. Commented Jun 21, 2023 at 22:40

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.