10

Poking around Magento 2's core code, it looks like the old class rewrite system has been ripped out and replaced with a new dependency injection system.

Unfortunately, there's no documentation for this new system.

Does anyone have a module configuration sample that would let a user, in Magento 2, "rewrite" the class Mage_Catalog_Model_Product with a new class Packagename_Modulename_Model_Product, using the new dependency injection system?

1

3 Answers 3

10

Di configuration was moved to di.xml files, and format was modified. Now preferences look like this (di.xml):

<config>
    <preference for="{Interface_Or_Class_Name}" type="{Preferred_Class_Name}" />
</config>
2
  • if it is a class rewrite, is it preferable to use a plugin instead of a preference node?
    – huzefam
    Commented Oct 29, 2015 at 12:14
  • 2
    If you need to substitute behavior of some core class, you should use preference. If you need to substitute behavior of some core method, you should use plugin with around listener that will not call $proceed(). If you need to add behavior to some core method or class, you should use plugins.
    – Anton Kril
    Commented Oct 31, 2015 at 14:54
5

Working from the inside out, I was able to get a rewrite working with the following

<global>    
    <di>    
        <preferences>
            <Mage_Catalog_Model_Product>Packagename_Modulename_Model_Product</Mage_Catalog_Model_Product>
        </preferences> 
    </di>
</global>

However, there's a lot more systems code in the dependency injection implementation, so it's not clear if the above will work in the final released version of Magento 2.

1
  • So it seems they basically renamed "alias" to preferences and moved it to the top? Interesting. I'm looking forward to seeing the updates to the documentation roll out. Peter said they have it, but it all hasn't been pushed out to the docs site, at least IIRC.
    – davidalger
    Commented Sep 19, 2013 at 13:21
5

You are correct, class rewrites were entirely eliminated and replaced with DI. The following notes are from the section on Application Framework changes:

Magento 1.x — Node: /global//{sub-path which corresponds to factory name + "rewrite" literal}

<global>
...
    <models>
        <core>
            <rewrite>
                <url>My_Module_Model_Url</url>
            </rewrite>
        </core>
    </models>
...
</global>

Magento 2.x — Node: /global/di/aliases

<global>
...
    <di>
        <aliases>
            <Mage_Core_Model_Url>My_Module_Model_Url</Mage_Core_Model_Url>
        </aliases>
    </di>
...
</global>

I haven't tried utilizing this in a module running in Magento 2.0, but it appears that you basically define an alias for the class name instead of using the complicated hierarchy of xml for per-module rewrites.

1
  • 1
    +1 for the Application Framework page, but this doesn't work in the latest Magento 2 snapshot I grabbed. Commented Sep 19, 2013 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.