Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [dependency-injection]

Denotes questions regarding Magento 2 Dependency injection

1
vote
1answer
22 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-...
2
votes
1answer
28 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 ...
2
votes
1answer
35 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 ...
0
votes
0answers
10 views

Trouble understanding Magento 2 context DI order in core code

In the module-customer/Controller/Adminhtml/Group , the Context class is passed into the __construct method 3rd to last, before the resultForwardFactory and resultPageFactory factory classes. ...
5
votes
1answer
130 views

How to bypass CSRF validation for certain requests like payment gateway webhook?

I am trying to integrate CC avenue payment method with Magento 2.3 however once payment is processed, user is navigated back to website with error "Invalid Form Key. Please refresh the page." Upon ...
0
votes
0answers
21 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 ...
0
votes
1answer
28 views

What is the Use of Type and Virtual Types(di.xml) in Magento2?

I want to know clear about Type and Virtual Types in di.xml with examples. When we can use it. Please share simple example to understand concept. Note please go through the practical examples.
0
votes
1answer
47 views

How many types of Injection methods does Magento 2 support?

What types of Injection methods does Magento 2 support? I am aware about, Constructor Injection - consider as a Injection. Method Injection - consider as a Injection. BUT And Interface and Model ...
0
votes
2answers
258 views

magento 2: Hide shipping method in frontend

I have the shipping method "Flatrate" enabled in backend. So, as expected, it is available in frontend, too. But this shipping method should only be available in backend. Of course, I already googled ...
1
vote
0answers
113 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. ...
0
votes
0answers
19 views

How to extend Advanced search result page's listing items

I have a module which a bit extends an appearance of listing items. It adds different labels to some products depends on various conditions. Everything works well on categories' listing pages and ...
0
votes
1answer
67 views

Magento 2: How to inject classes in custom script

I am working on some custom scripts to import some data before a new store is launched. These scripts are needed only once and will not play any role after launch. They are supposed to be executed on ...
0
votes
1answer
167 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\...
0
votes
1answer
27 views

Can I call addFieldToFilter via di.xml? / Set a global filter on product collection

I am working with vendor/smile/elasticsuite/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Fulltext/Collection.php which extends the \Magento\Catalog\Model\ResourceModel\Product\...
2
votes
1answer
329 views

Exception #0 (BadMethodCallException): Missing required argument $data

I have a module which is rewriting core module (module-fedex), the constructor is the following: public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \...
0
votes
1answer
238 views

Magento2: dependency injection issue in passing parameter to constructor

I have faced this issue many times, but unable to understand why? what is the (logic/concept/approach) behind injecting any object. i have injected few parameter to my save controller, before adding ...
0
votes
0answers
11 views

Do Magento 2 DI Helpers persist state across or through requests?

I was hoping to double-check my sanity around Magento's DI mechanism. Let's say I have X helper that is used in 3 places within the workflow of a single request, where X helper has multiple ...
0
votes
1answer
162 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 ...
0
votes
1answer
38 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 ...
1
vote
1answer
23 views

Injecting object into a pluigin

I just want to inject an object into my plugin. /* @var \Magento\Checkout\Model\Session */ protected $session; public function __construct(\Magento\Checkout\Model\Session $session) { $this->...
1
vote
1answer
100 views

Magento 2 : injecting models, interfaces in constructor magento

I'm new to magento 2 and I'm trying to understand how exactly injections work in magento. I understand that injecting a model and interfaces is the right approach in magento 2. But for some reason ...
3
votes
2answers
184 views

Magento 2: Does dependency injection affect performance?

I was wondering if dependency injection can be bad for the performance of the website. For example: Let's say that I'm building a helper class for my module and it contains a lot of useful methods ...
0
votes
2answers
589 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 ...
4
votes
2answers
1k views

Magento 2: 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 follow: <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
0
votes
1answer
395 views

Magento2 extend core block constructor

Let's say I have a helper class (let's call it \Vendor\Module\Helper\Cart) which I'd like to use in a block. I understand I should add the helper class in block constructor, so I should extend core ...
0
votes
1answer
94 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 ...
0
votes
0answers
131 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, ...
0
votes
1answer
127 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 ...
0
votes
1answer
274 views

Magento2 - controller redirect using plugins

Doing url redirects is an easy thing rewriting a controller class, as $this->resultRedirectFactory is accessible as it is one of default injected objects But, can we use this in plugins? I've been ...
0
votes
1answer
52 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 ...
2
votes
0answers
86 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 ...
3
votes
0answers
78 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 ...
0
votes
0answers
267 views

How to change the type of a constructor parameter through dependency injection

We are facing the following situation: We use a module that is not longer maintained by the developers, but we still would like to use this module. The problem is, the code is not compatible with ...
1
vote
0answers
32 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 ...
3
votes
0answers
358 views

Magento 2 Optimize 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\...
1
vote
1answer
200 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, ...
2
votes
1answer
1k views

Module isn't overriding default Mail Transport in Magento 2.2

I started developing Magento 2.2 modules about 3 months ago. I did a very simple SMTP module by following what some open source projects did. I could use a free plugin but the real goal here is to ...
1
vote
0answers
19 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 ...
2
votes
1answer
28 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 ...
0
votes
4answers
560 views

Is there any way to identify what class causes 'Area code is not set' or 'Area code already set'?

I know there are a lot of questions about 'Area code is not set', but no one has yet answered the main question. How to find the file which caused this Exception? In my case, I updated a lot of ...
2
votes
2answers
1k views

Dependency injection confusion Magento 2

I hope this is the correct place to ask the question for this. I read the tutorial about DI for Magento 2, I almost get it the concept. How Magento 2 use di in the framework. After reading I get to ...
3
votes
1answer
533 views

Does arguments order in constructor matters?

I've noticed strange situation while creating class contructor (__construct). Here is my code: public function __construct( \Magento\Framework\Model\Context $context, \Magento\...
1
vote
2answers
841 views

What preference to use for \Magento\Framework\DB\Adapter\AdapterInterface?

I'm trying to override a Collection constructor and getting Fatal error: Uncaught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in /var/www/magento/vendor/magento/...
0
votes
2answers
30 views

Inject class in custom Field

I created a backend configuration for my custom plugin /app/code/Sostanza/LiveHelp/etc/adminhtml/system.xml https://pastebin.com/2jwnYLwb /app/code/Sostanza/LiveHelp/Block/Button.php https://...
1
vote
1answer
148 views

M2 Circular dependency: CalculateOrders\Metric depends on CalculateOrders\Metric and vice versa

how can I fix this error ? Circular dependency: Heckfy\Health\Model\Metrics\CalculateOrders\Metric depends on Heckfy\Health\Model\Metrics\CalculateOrders\Metric and vice versa I have class metric ...
1
vote
1answer
322 views

Dependency injection not working on one the the tax collector class

I am trying to override mapItem method from "Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector" using below code. However code written in my local module's class is not invoked. VendorName\Tax\...
0
votes
1answer
473 views

How is the array $data parameter set in Magento 2 (dataObject constructor)

In https://github.com/SnowdogApps/magento2-menu/blob/master/Block/Menu.php, a plugin that makes it possible to replace the default menu, the constructor of the block looks like below. For all ...
3
votes
1answer
201 views

What is the difference between use Magento\… under namespace and __construct(Magento\…)? [duplicate]

As I'm writing a new module, this question just raise out my curiousity. Let's take Magento\Customer\Model\Customer as an example, the below codes can perform the same function actually: Using use ...
0
votes
1answer
403 views

Magento 2 Custom module error when changing constructor

I am creating a new custom module for Magento 2.1.x which some includes some Blocks. In one of them, at the beginning of the development, the constructor had onlt two parameters: use \Magento\...
3
votes
2answers
1k views

Magento 2: Observer constructor not working

I am creating an observer for an event in Magento 2 and followed all the steps in the documents I should follow. The observer is called after I registered it in the events.xml file. However, it doesn'...