Questions tagged [di]

Denotes questions related to Dependency Injection in Magento 2.

Filter by
Sorted by
Tagged with
36
votes
4answers
24k views

Magento 2: Run `setup:di:compile` **only** a Specific Module?

Is it possible to pre-generate code for one specific module? I.E. -- I can generate all the code in the system with php bin/magento setup:di:compile However, this can take a long time. I'd like to ...
21
votes
2answers
14k views

What are all the allowed `xsi:type` values in the xml's from Magento2

In Magento 2 (almost) all arguments listed in xml files have an attribute xsi:type that determine how the value of the argument is iterpreted. For example, in di.xml file of the backend module there ...
21
votes
2answers
6k views

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

So I noticed that in most models and blocks, there's this array $data = [] given as the last parameter of the constructor. For example \Magento\Catalog\Block\Product\ListProduct public function ...
20
votes
4answers
6k views

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

So, I know theorically what is a proxy class in Magento 2. I've read the awesome Alan Storm article about it and I totally understand how those classes are generated. However, and I don't know if it'...
18
votes
6answers
5k views

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

I have installed the new Magento 2.3.3 version. After I have run the command: php bin/magento setup:di:compile I have Faced the following error: PHP Fatal error: Interface 'Vertex\Tax\Model\...
15
votes
2answers
1k views

Magento 2: use statement versus direct class path?

I may be missing a point but I'm just wondering why sometimes there is an "use" statement for a specific class and sometimes we don't. Example: app\code\Magento\Email\Model\Template.php, we have at ...
14
votes
1answer
19k views

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

In Magento 2, What case I use di.xml and How to use di.xml for module? Anyone knew, please show me step by step.
12
votes
2answers
2k views

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

I don't understand why, in some classes, their dependency injections are declared twice - once in the di.xml and in the concrete class's constructor. For instance in Magento\Backend\Model\Url, its di....
10
votes
3answers
12k views

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

Given a plugin defined in di.xml such as the following: <type name="\Magento\Catalog\Model\Product"> <plugin name="se_catalog_product_plugin_ample" type="Example\Module\Model\...
10
votes
1answer
2k views

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

I currently have the following <preference/> in one of my di.xml file: <preference for="Magento\Contact\Controller\Index\Post" type="RadTest\TestModule\Controller\Contact\Post" /> I have ...
10
votes
1answer
702 views

The purpose of the http verbs interfaces

In Magento 2.3 there are some interfaces for all the http verbs Magento\Framework\App\Action\HttpPostActionInterface Magento\Framework\App\Action\HttpGetActionInterface, ... All of them are empty ...
10
votes
3answers
880 views

Magento 2: Use of priceModifiers

While browsing the code of magento 2.0.7's catalogrules module, i came accross a class Magento\CatalogRule\Model\Product\PriceModifier, Which was passed to Magento\Catalog\Model\Product\PriceModifier\...
9
votes
2answers
2k views

di.xml constant type vs init_parameter

I see in the di.xml files from the core that some of the arguments have the type init_parameter but the values of the parameters are all constants. <type name="Magento\Framework\View\Page\Config\...
9
votes
2answers
2k views

Magento 2 Doesn't Support Dependency Injection in Traits?

Do traits actually work with dependency injection in Magento? Consider the following code: Trait Class namespace Frame\Slick\Block; use Frame\Slider\Slick\Block\Data as Helper trait Slick { ...
8
votes
2answers
349 views

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

At this time I am annoyed of writing similar constructors en masse like the following within my modules. public function __construct( \Magento\Framework\Model\Context $context, \Magento\...
8
votes
3answers
633 views

What is the use of “mapping” node in di.xml in Magento 2?

I am using Magento 2.2.5 have a look on file vendor\magento\module-sales\etc\di.xml <type name="Magento\Payment\Model\Checks\SpecificationFactory"> <arguments> <...
8
votes
0answers
331 views

What is/means/does Magento\Framework\EntityManager\MetadataPool and Magento\Framework\EntityManager\Operation\ExtensionPool

There are new things we can see at Magento 2.3 in di.xml for service contracts. let's give an example:Magento_Cms module Like Magento\Framework\EntityManager\MetadataPool Magento\Framework\...
7
votes
2answers
2k views

What is dependency injection in magento 2

I am new in Magento 2. I wanted to know, what is dependency injection in magento 2. What the concept is about, and how to use it in our framework ? I have referred this documentation But still ...
7
votes
1answer
2k views

What goes in the global di.xml and what goes into the area specific di.xml?

I'm creating a Magento 2 module that adds some functionality to the backend. It does not touch the frontend at all. But if I place some virtual type in the etc/adminhtml/di.xml (the admin di file) ...
7
votes
1answer
775 views

What is/means/does `Magento\Framework\Model\OrchestratorPool`?

I found in di.xml from the catalog module this: <type name="Magento\Framework\Model\OrchestratorPool"> <arguments> <argument name="operations" xsi:type="array"> ...
7
votes
1answer
449 views

Magento 2: Cleaning up Unused Dependencies

Does anyone know of a code-cleaning/analysis tool that would inform a PHP Magento 2 programmer which DI arguments are not being used? For example, a tool that would look at the following class <?...
6
votes
2answers
2k views

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

It's pretty easy to replace a class in Magento 2 using dependency injection. However, let's say I have two modules that want to replace the same class. For example, the dotmailer Magento2 extension ...
6
votes
1answer
811 views

Magento 2: Why di compile is not 100%?

When we run php bin/magento setup:di:compile Why all are not will be 100% & 7/7? $ php bin/magento setup:di:compile Compilation was started. %message% 0/7 [>---------------------------] 0% ...
6
votes
2answers
7k views

How to Add a viewModel to a core block?

I am trying to overwrite a core template and add my custom logic using a viewModel as follows: <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&...
6
votes
1answer
3k views

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

How to run fast command line setup:di:compile. I was waiting so long, if can you show me why?
6
votes
2answers
425 views

Why auto generated Factories do not extends or implements any classes (interfaces)

This is more abstract question then specific problem. Why auto-generated factories in magento do not implements any common interface? And what bothers me maybe more. Why auto-generated factories ...
6
votes
3answers
1k views

How to debug Uncaught TypeError: Argument must be class A, class B given

First off, very importantly: This error ONLY happens in production mode. In developer mode it seems to be fine. So those of you who will tell me to clear var/generation: That would break production ...
5
votes
2answers
2k views

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

Looking at the core Magento2 modules in the vendor folder, I notice that there are di.xml placed in different folders etc/adminhtml/di.xml, etc/frontend/di.xml and etc/di.xml. The Magento_Cms module ...
5
votes
4answers
6k views

Magento 2 var/di folder is missing

There is no di folder generated in var directory. I am getting please re-run compile command. Why we need this di directory?
5
votes
1answer
330 views

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

I would like to understand the mechanism of running the setup:di:compile command in Magento 2. Example: Step 1: it is checking... Step 2: it is generating... ... Anybody can explain it to me?
5
votes
1answer
468 views

Magento 2 : Which File & Line app/etc/di.xml Config Will Load by App

I need to know, area wise config will load by application entry point files like /Magento/Framework/App/Http launch method. But I'm not able to figure it out app/etc/di.xml config load.
5
votes
1answer
1k views

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

Looking through Magento 2 di.xml files there are various types of xsi:noNamespaceSchemaLocation When should we be using each schema?
5
votes
1answer
6k views

Magento 2: Error during compilation Incompatible argument type

In Continue with Magento 2: Error during compilation I have below error while running php bin/magento setup:di:compile Errors during compilation: Custom\Module\Model\ResourceModel\...
5
votes
1answer
877 views

Change <item> node class via di.xml

If I have something like this: <type name="Magento\Downloadable\Ui\DataProvider\Product\Form\Modifier\Composite"> <arguments> <argument name="modifiers" xsi:type="array">...
5
votes
1answer
7k views

Magento 2 - Admin grid from file collection

I am actually trying to create a really simple grid (for now) listing files from server. The collection work well, I take as example Magento_Backup module but for the grid the module is using the old ...
5
votes
1answer
616 views

Swapping Less for Sass / Removing an argument previously added to type via di.xml in Magento2

So I am investigating Ben Mark's claim that is really easy to switch out the PHP Less pre-processor for a Sass one. https://twitter.com/benmarks/status/590219053437833217 (I don't buy it) I have ...
5
votes
1answer
966 views

Magento 2. Injecting a virtual type results in array instead of object

I'm having some trouble with virtual types. I've done some research and this is what I have. 0: A class which takes a string argument 1: A virtual type based on above class with a constant value for ...
5
votes
1answer
2k views

Magento 2 di.xml merging with arguments

I've been working through Magento U's Fundamentals of Magento 2 Development course, and I'm currently working on the second unit, which covers request processing. This morning, I was finishing the ...
5
votes
1answer
779 views

Using Dependency Injection in Magento 1?

I would love to use Magento 2's DI in some of my Magento 1 modules. I'm not entirely sure how can I approach that and more importantly, if Magento 1 will allow me to do so ? Most of the M1 classes ...
5
votes
2answers
2k views

“Invalid Factory for nonexistent class” (setup:di:compile error)

I get the following error when running bin/magento setup:di:compile: Errors during configuration scanning: IntegerNet\SolrSuggest\Implementor\Factory\AppFactory Invalid ...
5
votes
1answer
477 views

Plugin for aroundSavePaymentInformationAndPlaceOrder Throwing Error

I have a plugin that i want to intercept aroundSavePaymentInformationAndPlaceOrder, but it is giving me an error. Here is how it is set up: Vendor/Module/etc/di.xml <?xml version="1.0" ...
4
votes
3answers
6k views

Magento 2 custom logger 'Missing required argument $name'

I'm trying to create a custom logger, so I can log to a custom log file. I've looked at and followed multiple tutorials that all say the same, however I keep getting the same error. Tutorials that I ...
4
votes
1answer
6k views

Where are class constructor parameters defined in Magento2?

If you see AbstractExpress class for PayPal payment method, you could see: /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Customer\Model\Session $customerSession * @...
4
votes
2answers
1k views

Using 2 instances of the same class as dependencies for an other class

I've built a CRUD module in Magento2. My Save admin controller has 2 dependencies that are basically the same classes, with different settings. But when I run the static tests php bin/magento dev:...
4
votes
2answers
259 views

The order of the scoped config files

In Magento 2, you can have some config files be scoped. For example di.xml can be present in the etc folder of a module and/or etc/frontend and/or etc/adminhtml. Same goes for events.xml and maybe ...
4
votes
3answers
2k views

Overriding constructors for dependency injection

So, the new dependency injection approach requires me to inject everything I need into the contructor of an object. I get that. But how is this more flexible? For instance... I have my own Topmenu ...
4
votes
2answers
4k views

Magento 2: How can I use ifconfig or something similar to layout component

I created a boolean option (Yes / No) in Store -> Configuration, with which I can display or not the checkout sidebar. The configuration path is: vendorname_sidebar/display/enabled. Below is a part ...
4
votes
2answers
977 views

Magento 2 not respecting preference?

I'm trying to make a preference to override the PayPal Module's ResponseValidator class, so that i can save the validation errors for easier debugging on our end if a customer's checkout fails. The ...
4
votes
1answer
2k views

Magento 2 Plugin - ProductRepository getList override

I'm a beginner with Magento 2 and am having some issues with plugins - I've successfully done a plugin to change a simple object get statement (after event) - and in this case after compiling my code ...
4
votes
3answers
940 views

REST Api interface di not working. Class ItemInterface does not exist

ItemInterface: namespace MyVendor\SampleModule1\Api\Data; interface ItemInterface { /** * @return string */ public function getName(); /** * @return string|null */ ...