Magento 2 – Rewrite AccountManagement Model of Customer (ver. 0.42.0-beta2)

magento2magento2-dev-betamodel

I want to rewrite activate function of AccountManagement.I don’t know how to proceed.

According to documentation

A public interface is a set of code that third-party developers can call, implement, or pluginize. Magento guarantees that this code will not change in subsequent releases without a major version change.

Public interfaces for a module reside in the Api folder for a module. For example, the public interfaces for the Customer module reside in the app/code/Magento/Customer/Api folder. Third-party developers should use only these interfaces. You can use other interfaces but Magento does not guarantee that other interface will not be modified or removed in subsequent releases.

1.I need to rewrite AccountManagementInterface or AccountManagement?

2.It is necessary to use di.xml?

3.following is correct?

<preference for="Magento\Customer\Api\AccountManagementInterface"
                type="Mynamespace\Mymodule\Model\AccountManagement" />

Thanks in Advance.

Best Answer

The <preference> element in a di.xml file is the right approach. What you are doing is saying "use my type whenever you see a reference to the interface name in the merged di.xml file". So can supply any class that implements that interface. You could write this class from scratch (implementing the interface), or you could inherit the class we provide and override the one method with a new implementation (the class we provide implements the interface already).

Another approach is using plugins - you can intercept calls to an existing method without replacing the whole class, if that is appropriate. The advantage of plugins is you can add a bit of functionality to existing code easily, and if 3 different modules want to add functionality that will work as well. In comparison, only the last element will win - there can be only one class at the end of the day.

So you need to decide which approach best fits what you are trying to achieve. Since you are trying to rewrite a single function, you might want to look into plugins instead of using the <preference> element.

Related Topic