Stack Exchange Network

Stack Exchange network consists of 174 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
Make your voice heard. Take the 2019 Developer Survey now

Questions tagged [dependency-injection]

Denotes questions regarding Magento 2 Dependency injection

0
votes
1answer
39 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
89 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
68 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
17 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
38 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
136 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
0answers
44 views

Modifying the TopMenu + Counting Node Children

I'm modifying the Topmenu ( Magento\Theme\Block\Html\Topmenu ) to show an image on the right hand side of the drop down if there is sufficient space to do so, and space out the categories if not - I'...
0
votes
1answer
25 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
232 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
135 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
10 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
126 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
32 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
22 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
49 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
147 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
501 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 ...
2
votes
2answers
808 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
307 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
77 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
103 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
108 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
174 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
50 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
59 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
77 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
219 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
30 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
327 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
181 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
927 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
27 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
463 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
981 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
470 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
728 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
29 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
127 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
309 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
453 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
175 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
371 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'...
0
votes
1answer
313 views

Use of __desctruct() in classes to free memory

I have a suspected memory leak - I am reading from and writing to files within one class - perhaps the data isn't being released - not sure I need to run some tests. However, I am looking into using ...
0
votes
1answer
153 views

VirtualType not created / class not found (after cache clear)

Virtual Type XML Declaration: <virtualType name="SliderSlideImageUploader" type="Magento\Catalog\Model\ImageUploader"> <arguments> <argument name="baseTmpPath" xsi:type="...
3
votes
0answers
541 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 -...
1
vote
2answers
839 views

Magento 2: Helper not working

I am learning how to use dependency injections to get the classes I need. The following will work when creating a controller. ... use Magento\Framework\App\Action\Context; class Index extends \...
2
votes
1answer
7k views

Magento 2: How to get customer collection [closed]

I got customer collection with help of objectManager but I don't know how to get customer collection via injecting a class in constructor(Dependency Injection). So how we get customer collection ...
1
vote
0answers
670 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 ...