0

I am trying to inject additional dependencies into a class. The class in question is: \Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo

The construct of this class looks like:

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    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,
    Mapper $addressMapper,
    \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
    \Magento\Customer\Model\Logger $customerLogger,
    array $data = []
) {
    $this->coreRegistry = $registry;
    $this->accountManagement = $accountManagement;
    $this->groupRepository = $groupRepository;
    $this->customerDataFactory = $customerDataFactory;
    $this->addressHelper = $addressHelper;
    $this->dateTime = $dateTime;
    $this->addressMapper = $addressMapper;
    $this->dataObjectHelper = $dataObjectHelper;
    $this->customerLogger = $customerLogger;

    parent::__construct($context, $data);
}

I want to include 3 further classes without the requirement of copying the constructor in my class therefore in my module's di.xml I have:

<type name="\Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo">
    <arguments>
        <argument name="data" xsi:type="array">
            <item name="pricingHelper" xsi:type="object">Magento\Framework\Pricing\Helper\Data</item>
            <item name="myHelper" xsi:type="object">Vendor\Module\Helper\helper</item>
            <item name="myCollectionFactory" xsi:type="object">Vendor\Module\Model\ResourceModel\CollectionFactory</item>
        </argument>
    </arguments>
</type>

Then my class looks like this:

class myClass extends \Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo
{

    public function myFunction()
    {

        $collection = $this->myCollectionFactory->create();

    }

}

However, it's not working and I get the error of

Fatal error: Uncaught Error: Call to a member function create()

What am I doing wrong?

0

In your di.xml file, you are injecting your classes within an array wrapper called data.

So if I'm not mistaken to currently access the collection with your di it would be

$this->data['myCollectionFactory']

If you were to remove the data array wrapper to access them like you are trying to it would look like so.

<type name="\Magento\Customer\Block\Adminhtml\Edit\Tab\View\PersonalInfo">
    <arguments>
        <argument name="pricingHelper" xsi:type="object">Magento\Framework\Pricing\Helper\Data</argument>
        <argument name="myHelper" xsi:type="object">Vendor\Module\Helper\helper</argument>
        <argument name="myCollectionFactory" xsi:type="object">Vendor\Module\Model\ResourceModel\CollectionFactory</argument>
    </arguments>
</type>

Reference: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/build/di-xml-file.html#constructor-arguments


If you are looking at adding custom functionality to a template, that does not rely upon logic within the block (such as returning a custom collection etc) then another option could potentially be using a view model.

Where you can inject a custom class from XML and access it with $block->getMyViewModel()

See: https://www.yireo.com/blog/2017-08-12-viewmodels-in-magento-2

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.