Skip to main content
68 votes
Accepted

Magento 2 - What case I use di.xml and How to use di.xml for module?

What case We use di.xml ? We can use di.xml for ( rewrite ) preference of a particular class. We can send new or replace the existing class arguments. Use plugins to do some stuff before, after and ...
Krishna ijjada's user avatar
37 votes
Accepted

Magento 2.3.3 Fails on magento setup:di:compile with an error mentioning InvoiceFlexFieldProcessorInterface

Go to Below File /vendor/vertex/module-tax/Model/FlexField/Processor/OrderCurrencyGetterProcessor.php At the end of the "use" clauses in add the following two lines: use Vertex\Tax\Model\...
Rakesh Donga's user avatar
  • 5,364
35 votes

Is it possible to disable plugins defined in di.xml programatically?

In your di.xml you can do the following: <type name="\Magento\Catalog\Model\Product"> <plugin name="se_catalog_product_plugin_ample" disabled="true" /> </type> Magento does it ...
Raphael at Digital Pianism's user avatar
25 votes
Accepted

Magento 2: practical explanation of what is a proxy class?

This particular usage is not a good example of using Proxy pattern. I think it is even useless in that particular piece of code, as a collection is not doing any DB operations unless load method is ...
Ivan Chepurnyi's user avatar
25 votes
Accepted

di.xml constant type vs init_parameter

Found it. For const the value of the specified constant is used. For init_parameter, the value provided must be a constant name but the actual value used is the value of $_SERVER[constant value here]. ...
Marius's user avatar
  • 198k
18 votes

Magento 2: practical explanation of what is a proxy class?

A proxy class lets you dependency-inject a class that you won't necessarily need, and that has a high cost associated with doing so. If you look at a proxy Magento has generated, like \Magento\...
Ryan Hoerr's user avatar
  • 12.3k
17 votes

Magento 2: what is the $data array constructor parameter?

The $data[] can be used to hold values passed from di.xml's arguments replacement mechanism, when \Magento\Catalog\Block\Product\ListProduct intantiated. In your case this could be populated by <...
Codrain Technolabs Pvt Ltd's user avatar
17 votes
Accepted

How to dynamically enable/disable <preference> in di.xml?

you cannot enable and/or disable preferences based on a config setting. di.xml is just configuration. you cannot have logic in it, but you can do something else. You can have in your class a condition ...
Marius's user avatar
  • 198k
16 votes

Is dependency injection possible for plugins?

Late answer but you need to change __constructor to __construct and it will work perfectly well. Don't use static ObjectManager if possible. You should always use DI instead.
Luke's user avatar
  • 792
16 votes

Magento 2.3.3 Fails on magento setup:di:compile with an error mentioning InvoiceFlexFieldProcessorInterface

I run into the same problem. It seems only a speeling problem: https://github.com/magento/magento2/issues/24930#issuecomment-543949135 So it is better to change only the namespace spelling without ...
Mario's user avatar
  • 405
15 votes
Accepted

Why some classes define injections in both their constructor and di.xml?

As stated in the documentation, in Magento 2, the di.xml can be used to do the following: You can configure the class constructor arguments in your di.xml in the argument node. The object manager ...
Raphael at Digital Pianism's user avatar
15 votes
Accepted

Magento 2 custom logger 'Missing required argument $name'

I have encountered this problem as you had. The problem is the new configuration: <argument name="name" xsi:type="string">MageNinjaApiLogger</argument> in etc/di.xml is not updated. Run ...
thienphucvx's user avatar
  • 1,334
14 votes
Accepted

Magento 2: what is the $data array constructor parameter?

The $data can be used to populate data on your object since the constructor of \Magento\Framework\DataObject is this public function __construct(array $data = []) { $this->_data = $data; } or ...
Kristof at Fooman's user avatar
13 votes
Accepted

Magento 2: Dependency Injection: Optional class as parameter

First Approach: The cleanest way is to not take decisions in the constructor. Let the di.xml do that for you. Let's say the core class you are using if a module is not present is Some\Core\ClassName. ...
Aasim Goriya's user avatar
  • 5,454
11 votes
Accepted

Magento 2 - What is file Interceptor.php?

Magento 2 plugin system based on the Interceptor pattern. Calls to almost any module can be intercepted and altered. Vast improvement over the rewrite pattern in Magento 1, so obviously - no more ...
Toan Nguyen's user avatar
  • 3,059
11 votes
Accepted

Magento 2: Why di compile is not 100%?

As per my understanding. The whole process of setup:di:compile is divided into the 7(100%) sub code generations process. Proxies(1/7) Repositories(2/7) Service(3/7) Application(4/7) ...
Manthan Dave's user avatar
  • 9,826
11 votes
Accepted

What is dependency injection in magento 2

Inside a project, classes interact with each other. So we can say, a class depends on other classes. And there are 2 ways the connection can be made. The class locates its dependencies. The ...
Marius's user avatar
  • 198k
10 votes

Magento 2: practical explanation of what is a proxy class?

Magento 2 type autogenerated proxies can be used to "fix" design mistakes. That can be very handy. There are 2 use cases: Wrap an expensive object graph that might not be needed every time by the ...
Vinai's user avatar
  • 14k
10 votes
Accepted

What's the difference between di.xml in the different etc folders?

Each module can have a global and area-specific di.xml file. Magento reads all the di.xml configuration files declared in the system and merges them all together by appending all nodes. As a general ...
Vishwas Bhatnagar's user avatar
10 votes
Accepted

Magento : 2 "Argument passed must be an instance of AuthorizerobinFactory"

Try to remove an old generated files using rm -rf var/generation/* command (rm -rf generated/* for the Magento >=2.2) from the magento root directory, because magento pre-generates all class files ...
Siarhey Uchukhlebau's user avatar
10 votes
Accepted

How to Add a viewModel to a core block?

It is possible and it works as expected! The reason why it was not working was because I was not implementing the right interface in my viewModel class. All viewModels need to to implement ...
diazwatson's user avatar
  • 2,440
10 votes
Accepted

Cannot get customer session in Controller

I think you need do it : PS: "purpose of all these dependency injections" use Factory ." Example : ... protected $sessionFactory; ... public function __construct( ... \Magento\...
Eugene Kapelko's user avatar
9 votes
Accepted

What is the best way to resolve di conflicts in Magento 2?

It's the same problem like in M1 if modules overwrite the same class. You've to implement your own class which consolidate the changes of all conflicting modules in order to resolve it. Your basic ...
Vadim Justus's user avatar
8 votes
Accepted

Magento 2 - I would like to understand the mechanism of running the "setup:di:compile" command

Overview of code compilation Code compilation consists of all of the following in no particular order: Application code generation (factories, proxies, and so on) Area configuration aggregation (...
Toan Nguyen's user avatar
  • 3,059
8 votes
Accepted

Magento 2 - How to run fast command line setup:di:compile?

Well, unfortunately apart from improving your hardware there is no way you can make that command faster. This command is the Magento code compilation. Code compilation consists of all of the ...
Raphael at Digital Pianism's user avatar
8 votes
Accepted

Magento 2: Error during compilation Incompatible argument type

This is because your parent constructor has redundant arguments or not correct . In your case, need to remove arguments: $storeManager and $metadataPool. app\code\Custom\Module\Model\ResourceModel\...
Khoa TruongDinh's user avatar
8 votes
Accepted

What are the different xsi:noNamespaceSchemaLocation in Magento 2 and when to use them?

It depends on the type of XML file you create. Basically, all the XSD files can be found under the lib/internal/Magento/Framework folder, here is the list on 2.1.2: For example, if you create a etc/...
Raphael at Digital Pianism's user avatar
8 votes
Accepted

Can a virtual type be used as a dependency for a class?

What you're trying won't work because php must be able to find the class that you are giving in a constructor argument's type hint. This is part of the language and can't be overriden. Php is unaware ...
Aaron Allen's user avatar
  • 9,009
7 votes
Accepted

Annoyed by tons of classes for DI in constructors of Magento 2 - is there a better way?

Check out \Magento\Framework\Model\Context, referenced in your example. What you describe is exactly what it does. Magento uses similar Context objects throughout the core to shorten DI lists. The ...
Ryan Hoerr's user avatar
  • 12.3k
7 votes
Accepted

Magento 2 di.xml merging with arguments

See the question/answer Marius linked to: https://magento.stackexchange.com/a/139912/1905 The simple answer is that DI of the same scope gets merged together. DI of a higher scope overwrites a lower ...
Ryan Hoerr's user avatar
  • 12.3k

Only top scored, non community-wiki answers of a minimum length are eligible