Creating and Applying Drupal Patches
Creating a Drupal patch can be a confusing process. Here are my own notes for helping out my future self. The core solution has some other concepts around it that all work together to patch a module.
The Solution
The key to the solution, and described aptly as a 'good [best] practice' by Stef VanLooveren. The core of the solution has three basic steps:
- Clone or check out the module code
- Change the code as needed
- Use
git diff
and redirect output to create a.patch
text file.
Additional Notes
- Clone the node from the module's project repository into a working location in your Drupal working project.
- Install the module to be patched in a temporary location, for clarity and convenience.
- Create a temporary module directory at
path/to/drupal/modules/dev
-- or similar. - Within the module dev directory, clone the relevant working branch, sometimes in a custom named directory. Example using the Responsive Menu module:
git clone --branch 5.0.x. https://git.drupalcode.org/project/responsive_menu.git responsive_menu_5.0.x
- Enable the module as usual, using Drush or the UI:
drush en responsive_menu
Drupal should enable the module, despite the developer methods used to install it. - Edit the files of the module as needed to get whatever needed to be working, working.
- Use the CLI to output a patch file, like so:
git diff 5.0.x > responsive-menu-typed-config.patch
This one-line command takes the output ofgit diff
for the branch (which shows the changes made to the file/s) and outputs it to a text file: (whatever-you-name-it.patch
).
Applying a Patch with cweagans/composer-patches
Once the code part of the patch is ready, it can be applied to a module. Assuming you want to install the module in the traditional way, rather than use the development version installed in the previous step.
- Install the Composer Patches module. Follow the directions at the source documentation.
- Install using composer (recommended with
--dev
flag):composer require --dev cweagans/composer-patches
- Add the necessary code to the
composer.json
file:
{
"extra": {
"patches": {
"drupal/responsive_menu": {
"Description of the patch": "path/to/your/patch-file.patch"
}
}
}
} - Note: You need to determine where you will host patches for your site to complete the
path/to/your/patch-file.patch
.
Run the Module, Await Assistance
Hopefully, the patch is a temporary measure to allow the module to function until it can be fixed for real by the module maintainers. Keep an eye on any issues and progress made, and when the time comes, remove the patch.
Comments