See also this alternative approach: https://gist.github.com/cmtickle/8900629447429126ffd7ff84e56ec780#to-patch-code-in-appcode
If you need to patch an M2 extension that isn't available to be installed via Composer, and you're concerned about losing edits directly to files in app/code/<Vendor>
directory, you can install and then patch the extension locally using these steps:
-
Copy the extension into an arbitrary folder location like
app/code/packages/VendorName/ModuleName
(this assumes this module has acomposer.json
file with the package name ofvendorname/module-modulename
) -
Run this command to add your custom package location to the composer repositories list:
composer config repositories.vendorname/module-modulename path app/packages/VendorName/ModuleName
-
Require the extension:
composer require vendorname/module-modulename
-
Now that you've installed the extension via composer, you can patch any files using composer-patches without worrying about losing edits when you upgrade the extension with updates from the vendor. See this article for more about composer-patches.
Important: One thing to be aware of when using this approach is that if you are patching an extension in app/packages
, when you run composer install
locally, the patch changes will be applied to the files in app/packages
, and the changes will show as "modified" in Git. You'll be tempted to commit those changes, but you should not, because that will break the ability for composer-patches
to apply the patches when you do a clean clone of the repo and run composer install
later. Instead, you should reset the modifications each time you run composer install
locally.
Inspiration from this article and specifically this comment from @navarr.
Thank you