Hot answers tagged

55

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 around the function By using virtualTypes creating a sub-class of another class. Let us take a quick example from Magento 2 customer module. 1.Preference <...


38

I've found all types by checking <xs:extension base="argumentType" in *.xsd files. lib/internal/Magento/Framework/Data/etc/argument/types.xsd, these are base types: "array" "string" "boolean" "object" "configurableObject" "number" "null" lib/internal/Magento/Framework/ObjectManager/etc/config.xsd, can be found in di.xml files: "object" "init_parameter"...


26

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\FlexField\Processor\InvoiceFlexFieldProcessorInterface; use Vertex\Tax\Model\FlexField\Processor\TaxCalculationFlexFieldProcessorInterface; and run again Below ...


21

Short Answer: No. That is not possible with tools shipped with Magento now. Explanation: We have to distinguish code generation and compilation. Compiler does both. For both operations we need to read configuration of all other magento modules, otherwise their output will be incorrect or not full. Compilation for one module does not make sense, as magento ...


18

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 called. If their observer would be used in console command class as the dependency, then it makes sense to use a proxy. The proxy class should only be used when ...


17

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 out of the box in Magento/Backend/etc/adminhtml/di.xml with the following : <type name="Magento\Framework\App\Action\Action"> <plugin name="...


17

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]. In the method Magento\Framework\ObjectManager\Factory\AbstractFactory::resolveArgument metohod you will find this else if ($argument === (array)$...


16

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 <type name="\Magento\Catalog\Block\Product\ListProduct"> <arguments> <argument name="data" xsi:type="array"> ...


15

There is no technical reason to prefer one over the other, except if there are name conflicts (like different "Context" classes). But those can be solved with aliases and that's what I usually do: use Magento\Framework\Model\Context as ModelContext; I assume that in the core many methods, especially the constructors, were generated by tools like the ...


15

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 injects these arguments into the class during creation. The name of the argument configured in the XML file must correspond to the name of the parameter in ...


14

According to my researches, here is what I've found: The argument interpreter is created in the lib\internal\Magento\Framework\App\ObjectManagerFactory.php : protected function createArgumentInterpreter( \Magento\Framework\Stdlib\BooleanUtils $booleanUtils ) { $constInterpreter = new \Magento\Framework\Data\Argument\Interpreter\Constant(); $...


14

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\Framework\View\Layout\Proxy, you'll see it has all of the same methods as the original class. The difference is that every time any of those are called, it checks if ...


14

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 adding the two use-lines: namespace Vertex\Tax\Model\FlexField\Processor; FlexField instead of Flexfield It could make a difference for external functions that ...


13

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 similar for Magento\Framework\Api\AbstractSimpleObject /** * Initialize internal storage * * @param array $data */ public function __construct(array $data ...


13

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 that checks your config flag and does some action accordingly. I assume your class RadTest\TestModule\Controller\Contact\Post extends Magento\Contact\...


12

Magento's bin/magento executable is just a Symfony Console component. This means you can find the individual file reponsible for a single command. The naming is always the same so for setup:di:compile you can find the DiCompileCommand.php (setup/src/Magento/Setup/Console/Command/DiCompileCommand.php). It'll always run the exeucte() method so that's where we ...


11

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) Interceptors(5/7) Area Configuration(6/7) Interception(7/7) Each process shows its Compilation part(ex - 1/7) and its percentage(ex - 10%/100%). When single process end ,...


11

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 dependencies are provided to the class. Magento 1 takes the first approach. Here is a random example. In this example, the category model depends on the url ...


10

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 rewrite conflict! For example: DI.xml <config> <type name="{ObservedType}"> <plugin name="{pluginName}" type="{PluginClassName}" ...


10

Explanation To pass parameters to contructors you may use the direct injection mechanism in Magento2 (look for di.xml). Defining an argument in a class constructor in Magento 2 is very similar to Magento 1 Mage::getSingleton method. You will get a singleton class of the type you required in the constructor. Magento will just pass you a singleton instance ...


10

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 with their constructors. Generated classes extends the original classes and used by magento to call a plugins.


9

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.


9

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 rule, the area specific di.xml files should configure dependencies for the presentation layer, and your module’s global di.xml file should configure the ...


9

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 module structure - app/code/YourNamespace |- ConflictResolver |- etc | |- module.xml | |- di.xml |- Model | |- ...


9

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. And the class that should use this core class us My\Custom\ClassName. And the class that might not exist is MyExtension\PerhapsNotInstalled\...


8

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 (that is, optimized dependency injection configurations per area) Interceptor generation (that is, optimized code generation of interceptors) Interception cache ...


8

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 ArgumentInterface. namespace Vendor\CustomModule\ViewModel; class Class implements \Magento\Framework\View\Element\Block\ArgumentInterface { public function ...


8

I think you need do it : PS: "purpose of all these dependency injections" use Factory ." Example : ... protected $sessionFactory; ... public function __construct( ... \Magento\Customer\Model\SessionFactory $sessionFactory, ... ) { $this->sessionFactory = $sessionFactory; ... } public function ...


7

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 following in no particular order: Application code generation (factories, proxies, and so on) Area configuration aggregation (that is, optimized ...


7

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 only thing to keep in mind is that this should not be used to hide away bad architectural decisions. You should consider whether each of the classes you need 'all ...


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