0

I know i should not modify any code inside vendor folder. And the vendor folder should be ignored in the git server.

But what if i need to make some small changes for the core module of my Magento application, instead of rewriting the whole thing of core module?

3

2 Answers 2

0

You will need to create your own module to override some of the core functionality, specifically inside the di.xml file of your new module.

  • Create a new module in app/code/[Namespace]/[Module]
  • Setup your di.xml file inside app/code/[Namespace][Module]
  • Create your referenced objects that overrides the core files.

You have two options inside di.xml : overrides preferences or plugins.

  1. Preferences are most likely useful for supplying/overriding argument values for constructors, but can also be used to override desired methods of the classes. This method is closer to Magento 1 overrides than plugins.

  2. Plugins are the recommended way to change behaviour for methods. They use Interception, in summary they use methods that execute around the original method: Before Methods, After Methods, Around Methods. This means you can supply arguments, and update result of the method.

Examples

Preferences

  • Preferences are easy to setup: preference for='[Type]'

This refers to a new type in your module for DbStorage.php. This example just provides a new preference and overrides the $logger argument a new type (the type then has the information of the file location).

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <preference for="Magento\UrlRewrite\Model\Storage\DbStorage" type="YourNamespace\YourModuleName\Model\Storage\DbStorage"/>
       <type name="[Namespace]\[Module]\Model\Storage\DbStorage">
       <arguments>
          <argument name="logger" xsi:type="object">YourNamespace\FileLoggers\Loggers\PsrLogger</argument>
          </arguments>
       </type>
    </config>

Just arguments in di.xml (with core type unchanged)

  • However if arguments are the only thing you want to supply (without changing method behaviour of the core type) you can simply supply a value for the original type without implementing a new type:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <type name="Magento\Core\Model\Session">
          <arguments>
             <argument name="sessionName" xsi:type="string">adminhtml</argument>
          </arguments>
       </type>
    </config>
    

Plugins

Plugins have the following syntax:

  • di.xml file:

    <config>
       <type name="{ObservedType}">
       <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" />
       </type>
    </config>
    
  • Code

    // Original type
    class X{
            public function methodName($name);
            }
    
    // Your plugin type
    class YPlugin{
            public function beforeMethodName($name){
    
            }
    
            public function afterMethodName($name){
    
            }
    
            public function aroundMethodName($name){
    
            }
    }
    
  • The class examples:

    1. beforeSetName() method:

      class ProductAttributesUpdater{ public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name) { return ['(' . $name . ')']; } }

    2. afterLogin() method:

      class AuthLogger{

          private $logger;  
      
          public function __construct(\Psr\Log\LoggerInterface $logger)
          {
              $this->logger = $logger;
          }
      
          /**
           * @param \Magento\Backend\Model\Auth $authModel
           * @param null $result
           * @param string $username
           * @return void
           * @SuppressWarnings(PHPMD.UnusedFormalParameter)
           */
          public function afterLogin(\Magento\Backend\Model\Auth $authModel, $result, $username)
          {
              $this->logger->debug('User ' . $username . ' signed in.');
          }
      }
      
1
  • Please upvote if you found this information useful. Commented May 28, 2020 at 7:41
0

Magento can be installed using composer or you can also clone the Magento repository.

When using composer, a vendor folder is created (this is not specific to Magento in fact). Now, the content of the vendor folder is directly linked to the file composer.json at the root of your Magento install.

If you change any core code in the vendor folder by mistake, you can delete the entire folder vendor and run composer install, it will recreate the same codebase and your site will work like before.

However, if you do overwrite the vendor code, you do compromise all this, and needless to say your environment is a lot less robust.

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.