Questions tagged [dependency-injection]

Denotes questions regarding Magento 2 Dependency injection

57 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
4 votes
0 answers
880 views

Magento 2 : When running integration tests, how can I override some of the dependencies that are injected?

I am running an integration test for Magento 2, but I am at a certain point that my integration test needs a different dependency than my production code. Without going too much into detail as to why, ...
Giel Berkers's user avatar
  • 12.2k
4 votes
0 answers
2k views

Overriding or Extending Core Interfaces

I am trying to add a new getter and setter to \Magento\Catalog\Api\Data\CategoryTreeInterface. I have created a new interface - \Wildcard\CategoriesApi\Api\Data\CategoryTreeInterfaceand a new model -...
Wildcard27's user avatar
3 votes
0 answers
755 views

Magento 2 memory leak perfomance

I have a problem with the import data process. First: I have used the factory creator to create a Model set data and save but the problem when I have 400k Item to save. --- Constructor \Magento\...
HoangHieu's user avatar
  • 1,348
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
2 votes
1 answer
111 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
2 votes
0 answers
489 views

Speed up dependency injector compile

Since I have started developing with Magento 2 I've noticed, compiling the dependency injector with bin/magento setup:di:compile can take 40 seconds and up to 1 min 20s with xdebug enabled. I have ...
David Stone's user avatar
2 votes
0 answers
94 views

Why is there a DI preference for ProductInterface?

The ProductInterface has a DI preference leading it the ProductModel. While ProductModel is marked as falling under the Backward Compatibility Standard (it is marked with @api), it is still belonging ...
Jisse Reitsma's user avatar
2 votes
0 answers
85 views

Magento 2: Best practice create model class

First thing, I have a question on this topic Optimize load model memory Then I have some questions for the best way to create a Model to load or save data to Database? Could you please explain which ...
HoangHieu's user avatar
  • 1,348
2 votes
1 answer
34 views

How does M2 know what to inject based on our _construct?

public function __construct( \Magento\Catalog\Helper\Category $categoryHelper, .... Let's say we have code like that. How does Magento know to inject category helper and lets us use it? It ...
Robot__4's user avatar
  • 119
2 votes
1 answer
1k views

How can I inject data into a class using dependency injection XML?

I'm under the impression that we can inject data into classes using di.xml and the $data array dependency. I'm struggling with getting it working however. Am I not understanding the concept correctly? ...
Danny Nimmo's user avatar
  • 1,243
2 votes
1 answer
386 views

Registering a shared service with a custom factory

I'm wondering how (if I can) register a shared service with a custom factory. Consider the following code: class MyApiFactory { private $config; public function ___construct(Config $config) ...
Aydin Hassan's user avatar
2 votes
1 answer
463 views

Magento 2 : How can I add an extra path to the DirectoryList

I want to be able to make Magento aware of an extra folder in my root called private_files. I thought I could do this by adding the following to my di.xml: <!-- Make Magento aware of an extra ...
Giel Berkers's user avatar
  • 12.2k
2 votes
0 answers
133 views

How do I determine the class name of a model?

This may sound like a completely dumb question, however I have been scavenging the web for answers with no success. If need to access a particular kind of model in Magento 2, such as a product model, ...
Chris A's user avatar
  • 21
1 vote
1 answer
472 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
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
59 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
  • 443
1 vote
0 answers
119 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
1 vote
0 answers
39 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
208 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
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
1 vote
1 answer
128 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
1 vote
1 answer
52 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
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
1 vote
0 answers
104 views

Magento 2 getting interface implementation from admin config

I have an interface which gives me the country in which the customer is located. interface CustomerLocatorInterface { public function getLocalCountryCode(); } And I have two possible ...
engbmaso's user avatar
  • 329
1 vote
0 answers
440 views

Circular dependency: Magento\Backend\Helper\Data depends on Magento\Backend\Model\Url and vice versa

I got error in magento 2 like Circular dependency: Magento\Backend\Helper\Data depends on Magento\Backend\Model\Url and vice versa. clearing var/di, var/generation and var/cache didn't help. ...
Dipak Parmar's user avatar
1 vote
3 answers
2k views

Fatal error: Uncaught Error: Call to undefined method getDirectoryRead()

I have a controller that works and does what i need it to do if i manually add a path like: $mediaImages = '/magento/pub/media/images/'; But i cant get the DirectoryList injection to work, it returns ...
Bossman's user avatar
  • 163
1 vote
1 answer
403 views

Only dependency assignment operations are allowed in constructor. No other operations are allowed

MEQP2: How to resolve below issue? abstract class ClassName extends \Magento\Backend\App\Action { public function __construct( ... \Magento\Catalog\Helper\Product $productHelper, ...
Bhavik's user avatar
  • 1,240
1 vote
0 answers
23 views

Class instatiation with data

I have created custom Factory class for my custom model class. Of course basically you can instantiate it by calling $this->objectManager->create(Someobject::class);. But, ObjectManage has also ...
Bartosz Kubicki's user avatar
1 vote
1 answer
2k views

Injecting Guzzle with Parameters Magento 2

Writing some REST API bits and going to use Guzzle, which I have used many times before. The issue I am coming across is, I know Guzzle has to take the configuration via parameters when constructing ...
Tom Burman's user avatar
1 vote
0 answers
825 views

Compile: Extra parameters passed to parent construct: $context

This has been asked a few times I know and I studied all of them and I also get the ieda what went wrong. However I cannot adapt those solutions to my issue. I have a Class Post which extends \...
hallleron's user avatar
  • 355
1 vote
0 answers
17 views

how dependency injection makes testing easier?

i read so many articles regarding di ,but there are no proper examples to understand in terms of testing. i think so many developers don't have idea on usage of di in magento2. anyone give one ...
Siva Kumar Koduru's user avatar
1 vote
0 answers
964 views

Circular dependency: Magento\Framework\App\Action\Context

I have a custom module. On 1 installation (all others work) I get the error, that there's a circular dependency. YES: Magento\Framework\App\Action\Context is a dependency in my Controller because ...
Philipp Sander's user avatar
1 vote
1 answer
52 views

Dependency Injection? Do not understand it, can anyone help?

I started to use Magento 2.x and noticed that the Mage "API" is no longer a reality, and red about dependency injection and factory but I did not understand what it is, what it does or where I write ...
user avatar
0 votes
1 answer
68 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
0 votes
0 answers
181 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
180 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
0 votes
1 answer
371 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
0 votes
1 answer
158 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
129 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,330
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
611 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
0 votes
1 answer
395 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
0 votes
1 answer
358 views

Weird error after Magento 2.2.6 update - Magento does not create ImageFactory class (and others)

After each an every try to update a particular Magento 2.2.5 installation (CE) to 2.2.6 we get the following error: PHP message: PHP Fatal error: Uncaught Error: Call to undefined method Magento\...
fritzmg's user avatar
  • 747
0 votes
1 answer
400 views

DI Compile fails with "Area code not set" error

I'm trying to use the \Magento\Sales\Model\InvoiceOrder class to invoice some orders. If I include the class in my constructor, however, then I get the "Area code not set: Area code must be set before ...
Colin Pickup's user avatar
0 votes
1 answer
126 views

Observer calling class in construct 500 error

I have an observer, which is working fine (after some effort on what is a Frankenstein website). However when I attempt to use my class in my construct it causes a 500 error (mode is developer, but no ...
Aravona's user avatar
  • 367
0 votes
1 answer
462 views

Magento 2 Category Collection doesn't show items not in Top Menu

I am trying to display certain categories along with their images on homepage. I've made sure the categories dont appear in the top menu and only in the code that I am trying to use. Below code only ...
Muhammad's user avatar
0 votes
1 answer
232 views

Magento Vagrant installation does not generate classes

I have switched to windows for a new project, and just setup Magento 2 using this vagrant box: https://github.com/paliarush/magento2-vagrant-for-developers I have it working, it is displaying ...
Digitaq's user avatar
  • 151
0 votes
1 answer
90 views

Inject class for overridded class

I override the \Magento\Catalog\Block\Product\View it's working but I am trying to inject class means it's not working please tell me what I did wrong and tell me how to do this without injecting the ...
Jeeva Chezhiyan's user avatar