Framework files in Magento2

When you want to serve personalized experience to your shoppers by making changes to the default layout, it always ends up at Magento Customization. Over the time Magento has topped the Ecommerce CMS list with its functionality, features, and power of extending the native functionalities. Compared to Magento 1, In Magento 2 overriding and manipulating with classes is becoming uncomplicated a developer to play speedily. At Webcreta Technologies, we have never suggested modifying default core functionalities, but we can extend core functionalities using concepts of overriding which is a safe and easy way to do customization or development.

By default, there are three types to override core files in Magento 2:

  • Preference
  • Plugin
  • Event-observer

In Magento2, While using overriding, sometimes you need to override the class, which resides in the framework. At that time, with the help of this tiny piece of code inside your custom extension, it will be easy for you.

For example, we need to override the Factory.php of “vendor\magento\Framework\Message”.

Then create di.xml in “app\code\Module\ModuleName\etc” folder and add below code in this file

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\Message\Factory" type="Module\ModuleName\Framework\Message\Factory" />
</config>

 

Now create Factory.php file in “app\code\Module\ModuleName\Framework\Message” folder and add code as follow:

 

<?php
namespace Webcreta\AddInformationMessage\Framework\Message;

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Message\MessageInterface;
use Webcreta\AddInformationMessage\Framework\Message\WebMessageInterface;

/**
* Message model factory
*/
class Factory extends \Magento\Framework\Message\Factory
{
/* You can add or edit anything in this file */
}

That’s it! You have successfully overridden the framework files in Magento 2. You are free to play and manipulate this according to your need for overriding one or more classes in Magento 2 Extension.

Leave a Reply