Question

I have a block which extends/overwrites the Magento block:

\Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo

This works perfectly. However in one of my classes I want to access a Helper, and so I added the consutructor

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Vendor\Name\Helper\SomeHelper $someHelper,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->someHelper = $someHelper;
}

public function getSomething(){
    return $this->someHelper->getSomething();
}

Fatal error: Uncaught TypeError: Argument 2 passed to Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo::__construct() must implement interface Magento\Customer\Api\AccountManagementInterface, array given, called in \app\code\retracted\retracted\Block\AdminOverrideBlocks.php on line 144 and defined in \vendor\magento\module-customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo.php on line 126 ( ! ) TypeError: Argument 2 passed to Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo::__construct() must implement interface Magento\Customer\Api\AccountManagementInterface, array given, called in \app\code\retracted\retracted\Block\AdminOverrideBlocks.php on line 144 in \vendor\magento\module-customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo.php on line 126

I know that the helper works, thats no problem. But this constructor overwrites the one on the Orginal Magento block - which causes errors.

I don't really want to copy and paste the entire constructor from the Magento block for various reasons (bound to cause issues down the road, not exactly elegant) and even when I tried this, I couldn't get past all the errors.

Is there a way to do it as a above but have the constructor inherited as opposed to overwrite? Or is there another way to access the helper without the requirement of the constructor?

Was it helpful?

Solution

You're getting this error because the parent constructor is being called with insufficient parameters. The arguments you pass to your constructor must have all the arguments used by the class you're extending so that your call to the parent constructor can have the complete set of arguments. A good IDE such as phpstorm does this for you.

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Customer\Api\AccountManagementInterface $accountManagement,
    \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
    \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
    \Magento\Customer\Helper\Address $addressHelper,
    \Magento\Framework\Stdlib\DateTime $dateTime,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Address\Mapper $addressMapper,
    \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
    \Magento\Customer\Model\Logger $customerLogger,
    \Vendor\Name\Helper\SomeHelper $someHelper,
    array $data = []
)
{
    $this->someHelper = $someHelper;
    parent::__construct($context, $accountManagement, $groupRepository, $customerDataFactory, $addressHelper, $dateTime, $registry, $addressMapper, $dataObjectHelper, $customerLogger, $data);
}

OTHER TIPS

Have you tried to clear the var/generation folder ?

You can also do that by installing n98-magerun2:

./n98-magerun2.phar generation:flush

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top