Questions tagged [dependency-injection]

Denotes questions regarding Magento 2 Dependency injection

Filter by
Sorted by
Tagged with
0 votes
1 answer
39 views

Is it possible to override type configuration in the di.xml of a module in Magento2?

I want to override the file "Fooman\EmailAttachments\Observer\AbstractSendInvoiceObserver". Here is the code in the di.xml. <!-- module introduced preferences --> <preference for=&...
Jancy Abraham's user avatar
0 votes
1 answer
55 views

How to remove the Deprecated property of __construct from Abstract Class using overriding or any other way

Error main.ERROR: Deprecated Functionality: Creation of dynamic property Magiccart\Magicproduct\Controller\Adminhtml\Product\Index\Interceptor::$_resultRedirectFactory is deprecated in vendor/...
Sheri's user avatar
  • 140
1 vote
2 answers
75 views

How and when should I use the Magento 2 commands bin/magento setup:upgrade, bin/magento setup:di:compile, and bin/magento static:content:deploy -f?

I'm new to Magento 2 and I'm trying to understand the purpose and appropriate usage of some important commands. Specifically, I would like to know more about the following commands: bin/magento setup:...
user avatar
1 vote
2 answers
314 views

__construct(...) and "automatic" dependency injection

I have recently been asked to help maintain a Magento module, and while I generally understand what DI is and why we want to use it, I was a bit confused by the 'automatic' side of things in Magento. ...
user111598's user avatar
1 vote
1 answer
407 views

Magento 2 di.xml reuse array arguments

In Magento 2 di.xml we can inject an array to a class like this: <type name="Vendor\Module\Model\Foo"> <arguments> <argument name="data" xsi:type="array&...
Hieu Duong's user avatar
0 votes
2 answers
119 views

Why doesn't work Dependency Injection from example?

I have tried to create DI like there (Data and DependentClass) but it doesn't working for me: "ArgumentCountError: Too few arguments to function DependentClass::__construct(), 0 passed...." ...
camradeq's user avatar
0 votes
1 answer
764 views

Call to a member function getById() on null in plugin class

I'm creating a plugin beforeAddProduct, $this->_productrepository is null finally, i've already runt setup:upgrade and cleaned generated directory here is the source code <?xml version="1.0&...
hkguile's user avatar
  • 2,221
1 vote
1 answer
2k views

How to override Magento 2 graphql resolver with custom plugin

Im trying to override the resolver by using a plugin in Magento 2 to change text. vendor/magento/module-quote/Model/CouponManagement.php - I need to change this class ApplyCouponToCart implements ...
Kbee's user avatar
  • 31
1 vote
0 answers
58 views

magento 2 is the position of parent::costruct() relevant?

I'm looking at Magento 2 core code. When Magento needs to extend a class, it creates a class with a constructor that calls the parent construct via the parent::__construct() method, something like ...
Lou Nik's user avatar
  • 433
0 votes
1 answer
26 views

Blank page when trying use direct injection with ProductRepositoryInterface class

I am fairly new to Magento and following a tutorial on Udemy to learn how to develop on Magento 2. I am attempting to overwrite the ProductRepositoryInterface, however whenever I add the preference ...
Peter Stuart's user avatar
0 votes
1 answer
518 views

Why is parent::__construct($context) used?

I have experience in programming with php and i think i have a good understanding of what a constructor does. But in Magento the constructor is called with the implemented Dependency's but after that ...
BigJonna's user avatar
0 votes
0 answers
174 views

Custom model save data processor DI/Reflection only working once?

I'm working on a simple FAQ widget module for a client and I seem to have run into something quite strange with the DI/Reflection cache. My problem is with the Save controller action which uses a ...
aweisman's user avatar
0 votes
1 answer
331 views

Magento 2 - Product gallery magnifier not working after overriding Gallery class

My magnifier is not showing anymore IF THE PRODUCT HAS MULTIPLE IMAGES, after I have overriden the gallery class! I have enabled the magnifier in THEME\etc\view.xml: <?xml version="1.0"?&...
Black's user avatar
  • 3,280
0 votes
2 answers
283 views

Inject class into plugin

I created an plugin for afterGetProductPriceHtml() and it works so far. My di.xml: <?xml version="1.0"?> <config> <type name="Magento\Catalog\Block\Product\...
Mario's user avatar
  • 478
0 votes
1 answer
179 views

Magento 2 cannot override 3rd party controller

Im trying to override a controller from swissup/firecheckout location in my vendor folder my di.xml is <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-...
mendz's user avatar
  • 143
1 vote
1 answer
121 views

Magento2 - How do I write below query function into non-deprecated? without using getCollection()

How do I write below function into non-deprecated? In Magento2. without using getCollection() public function getNewCustomer() { $customerData = $this->customerFactory->create()->...
Paul Fan's user avatar
  • 654
0 votes
1 answer
73 views

How do I change not using ObjectManager directly? and use Dependency injection?

How do I change not using ObjectManager directly? and use Dependency injection? what about my SQL query? My code is work, but seems not in good Magento style, anyone can help to correct me? I am ...
Paul Fan's user avatar
  • 654
1 vote
0 answers
118 views

RequireJS module - abstract dependency injection in Magento 2

I am searching for a way to extend a JS file which is used as dependency in other JS files for the whole project. The file in use is Magento_Ui/js/form/element/abstract and I need to replace it with ...
kanevbgbe's user avatar
  • 145
0 votes
1 answer
354 views

How can I get the store code inside a block in the product page using the dependency injection?

I have to check which is the current store view. In order to do that I was using the Object Manager to get the store code like this: $storeCode = \Magento\Framework\App\ObjectManager::getInstance() ...
KaMZaTa's user avatar
  • 205
1 vote
1 answer
3k views

Magento 2 - Using Object Manager

I have a module that places some shipping information into the order view of the admin. I am trying to display the current shipping method in this section. I have this in view/adminhtml/templates/...
stanhook's user avatar
  • 131
0 votes
1 answer
114 views

Understanding Composition with Magento

I am having a hard time trying to fully understand Composition with Magento. I am hoping you could help me with it. From what I gather, we no longer should use inheritance, so my classes should not ...
NetStorm's user avatar
  • 108
0 votes
1 answer
43 views

How many ways to do dependency injection?

How many ways to do dependency injection? Object Manager,Constructor injection and Method injection Is that it ?
Giga Todadze's user avatar
1 vote
3 answers
2k views

Magento 2 di.xml Parameter configuration inheritance explanation with example

I read about the Parameter configuration inheritance in Magento documentation but not able to understand the concept properly! https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-...
Bhaumik Upadhyay's user avatar
1 vote
0 answers
35 views

Is it possible to retrieve dependency injected arguments in another class?

Is it possible to retrieve dependency injected arguments in another class? For example, we have a class Vendor\Module\Block\Autocomplete. // app/code/Vendor/Module/Block/Autocomplete.php namespace ...
Quisse's user avatar
  • 860
1 vote
1 answer
706 views

How to Inject a new Class to Overridden Class using Preference

I want to override __prepareLayout function from 'Magento\Sales\Block\Order\History' so I used the preference. In my di.xml <preference for="Magento\Sales\Block\Order\History" type="...
User 69's user avatar
  • 145
1 vote
1 answer
67 views

Type Error occurred when creating object, attempting to use custom model from custom module

Yes I know, another "Type Error occurred when creating object" post... In addition, I'm probably going about this in a way that will make the seasoned M2 dev need a moment before reading ...
joeybab3's user avatar
  • 378
0 votes
2 answers
148 views

Magento 2 - Incompatible argument type, even though constructor parent call matches extended class

I try to extend this class from my own module, because I need to inject another dependency to the constructor and add a few new methods: vendor\aheadworks\module-wbtab\Block\Wbtab\ProductList.php ...
Black's user avatar
  • 3,280
0 votes
1 answer
156 views

How to get product attribute without using Object manager?

How to get product attribute of a product without using Object manager directly, since using Object manager is not recommended?
Mr Zid's user avatar
  • 438
0 votes
1 answer
150 views

Magento 2 - Injecting a model into a class? How to?

I have a class where I'd like to inject a model into it, to use the code already created in the model. How would I go about doing this in my class? Thank you.
James's user avatar
  • 281
0 votes
2 answers
125 views

Magento 2 - overriden class extending overriden class not working

I have overriden the class Magento\Catalog\Block\Product\ProductList\Upsell with my own Company\Base\Block\Product\ProductList\Upsell. The class Magento\Catalog\Block\Product\ProductList\Upsell ...
Black's user avatar
  • 3,280
1 vote
4 answers
9k views

Magento 2: Type Error occurred when creating object

I had created a custom API for adding new customer in Magento 2.3. I had injected dependency injection and passed the method to create the customer account. But it gives me below error: main....
IntraPersonalLearner's user avatar
1 vote
1 answer
1k views

Magento 2 Injecting Object in $data array

I am trying to inject my custom object in constructor like this in di.xml <type name="Magento\CatalogSearch\Block\Advanced\Result"> <arguments> <argument name="data" xsi:...
Amit Singh's user avatar
  • 1,745
0 votes
1 answer
1k views

Override Helper in Magento2 - Vendor\Module\Helper\Data doesn't extends Magento\Framework\App\Helper\AbstractHelper

May be i'm missing something really small. Here is a class Class1 namespace Vendor\Module\Helper; class Class1 extends AbstractHelper { /** @var Client */ private $client; /** @var ...
Kingshuk Deb's user avatar
  • 1,204
0 votes
1 answer
595 views

Injecting Helper causes "Undefined constant" and "'generated' directory permission is read-only"

When I inject the helper class it gives me the below errors. If I remove the reference to the Helper class in the constructor then everything works perfectly. controller file: /var/www/html/app/code/[...
Brenda.ZMPOV's user avatar
0 votes
0 answers
86 views

Magento2 - Listen for order new comments

I am trying to create some kind of observer that will listen for new order comments and execute the function every time a comment is added. I tried three ways that did not work for me: Observer ...
Gustavo Ulyssea's user avatar
1 vote
1 answer
202 views

What is use case to for directly calling new in Magento2

I know you should now init object using new keyword as it surpasses the DI But in few cases in core code is have observed the use of new keyword like in class module-catalog/Helper/Product Do any ...
Vishwas Bhatnagar's user avatar
0 votes
1 answer
387 views

Magento 2 email validation error with zendframework validator

I created an observer to send emails at a particular event. I used transport builder to send an email, but I get zendframework validator errors, but the email address is OK. I was able to solve the ...
Gustavo Ulyssea's user avatar
1 vote
4 answers
3k views

Order beforeSave plugin throws TypeError

I've built a plugin that catches the order beforeSave event in order to add some extra data to the an order attribute. The problem is that when this plugin fires it causes an error: Uncaught ...
Matthew's user avatar
  • 183
1 vote
0 answers
1k views

How to resolve Circular Dependency when one Helper class is depends on other Helper Class

Having one module named Override\Search in which two helper classes are there. 1. Override\Search\Helper\Data.php 2. Override\Search\Helper\Util.php Both helper are depends on each other, thus ...
Softec's user avatar
  • 2,065
6 votes
2 answers
2k views

Why I still need to run setup:di:compile in developer mode?

Quick Intro I'm working on a payment module and using Magento 2.3.2 (irrelevant). As stated in many places, we don't need to run setup:di:compile when in developer mode (which is my scenario). As ...
Ricardo Martins's user avatar
1 vote
1 answer
654 views

Injecting additional dependencies into a class - Magento 2.3

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 ...
Goose84's user avatar
  • 2,581
2 votes
1 answer
1k views

Magento 2 : di.xml on custom param won't work

I've a class located in \Vendor\Module\Model which is named Test. Here is the constructor of this class : public function __construct(array $map = []) { var_dump($map); die; } I created a di....
help's user avatar
  • 21
1 vote
1 answer
120 views

Dependancy injection and shared/unshared object and cron job

Something interesting happens to Magento2 code and I cannot realise what I missed. The same code works flawlessly via browser and does not work via cron. (in adminhtml area) I calculate the result in ...
vistore's user avatar
  • 36
4 votes
1 answer
81 views

What exactly is Dependency Injection in Magento?

I'm learning Magento 2 and couldn't able to understand some of the below things Dependency Injection Javascript bundling (Because some suggest to enable and some don't) Third party module override ...
Siva's user avatar
  • 1,120
1 vote
1 answer
50 views

magento 2 how dependency injection worked

I know this question is asked many time by another user but all of them describe "how we can use magento DI" ? they does not speak about how magento pass object that constructor method needed or how ...
gh darvishani's user avatar
0 votes
1 answer
1k views

Magento 2 circular dependancy error on upgrade

I'm currently running a website on Magento 2.2.7 When we add any new module and run the php bin/magento setup:upgrade command the admin panel immediately stops working and gives us an error beginning ...
CJNotts's user avatar
  • 392
0 votes
2 answers
216 views

Magento 2 Model Classses are not injecting without Factory in custom module

I am developing an module in Magento 2.I have created helper,Model Fatal error:vendorname\modulename\Model\Testclass class is not exist.I have tried each and everything to resolve this issue but ...
akgola's user avatar
  • 2,917
1 vote
2 answers
42 views

DI not working as expected

I'm trying to get an instance of Magento\Cms\Api\GetBlockByIdentifier, however I am receiving a value of null. I notice the CMS module defines a preference for this interface vendor/magento/module-...
quickshiftin's user avatar
  • 1,416
2 votes
1 answer
110 views

Magento 2: best practice building classes

I need some advice how to structure my code in magento 2. I am working on an ERP interface to import and export products, customers and orders. So I need some helper classes to take care of some ...
omsta's user avatar
  • 698
3 votes
1 answer
8k views

Get category name by ID

This is my Vendor/Module/Block/BlockFile.php <?php namespace Vendor\Module\Block; class CategoryList extends \Magento\Framework\View\Element\Template { public function getId() { return ...
Rufus's user avatar
  • 621

1
2 3 4 5