Skip to main content
alexb.me

Main navigation

  • Home
  • Hello
  • Stuff
    • Stuff Page
  • Junk
    • Junk Page
  • Blog
User account menu
  • Log in

Breadcrumb

  1. Home

Patching a Drupal Contrib Module

By alexb7217, 28 November, 2024

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: 

  1. Clone or check out the module code
  2. Change the code as needed
  3. Use git diff and redirect output to create a .patch text file.

Additional Notes

  1. Clone the node from the module's project repository into a working location in your Drupal working project.
  2. Install the module to be patched in a temporary location, for clarity and convenience.
  3. Create a temporary module directory at path/to/drupal/modules/dev -- or similar.
  4. 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
  5. Enable the module as usual, using Drush or the UI:drush en responsive_menuDrupal should enable the module, despite the developer methods used to install it.
  6. Edit the files of the module as needed to get whatever needed to be working, working.
  7. Use the CLI to output a patch file, like so:git diff 5.0.x > responsive-menu-typed-config.patchThis one-line command takes the output of git 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.

  1. Install the Composer Patches module. Follow the directions at the source documentation.
  2. Install using composer (recommended with --dev flag):composer require --dev cweagans/composer-patches
  3. Add the necessary code to the composer.json file:
    {
        "extra": {
            "patches": {
                "drupal/responsive_menu": {
                    "Description of the patch": "path/to/your/patch-file.patch"
                }
            }
        }
    }
  4. 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.

  • Log in or register to post comments

Comments

Hello!

How ya doin?
Remove
RSS feed
Powered by Drupal