13

I need to include a patch in my project. I have included cweagans/composer-patches in composer.json and added the following lines in the extra section (example is from when trying to enable distribution in the Configuration installer).

"patches": {
        "drupal/config_installer": {
            "Enable distribution for Configuration installer profile": "patches/config_installer_enable_distribution.patch"
        }
    }

Afterwards, I run composer update.

- Installing drupal/config_installer (1.5.0): Loading from cache
- Applying patches for drupal/config_installer
    patches/config_installer_enable_distribution.patch (Enable distribution for Configuration installer profile)

So I get no errors and the patch seems to have been applied successfully. Also the file PATCHES.txt is successfully created in the package's root directory. Still, the patch hasn't been applied.

I've tried this method with other packages which come from Github and it works, however every time I try this with a module or a profile from drupal.org then the patch isn't being applied.

Am I missing anything?

When I ran composer update -vvv, I saw the command which is running for the patch is indeed with Git.

git -C 'web/profiles/contrib/config_installer' apply '-p1' '/path/to/project/patches/config_installer_enable_distribu‌​‌​tion.patch'

I've tried running the command by itself; again, it didn't change the file.

11
  • Are you positive the patch hasn't been applied? Have you applied it manually to the same file/directory and confirmed there's a different result? It would be weird for the patch attempt to raise no errors/feedback but still fail
    – Clive
    Dec 1, 2017 at 12:01
  • What operating system are you on? If you are on OSX you will need to install gpatch. Are you sure the patch path is correct?
    – Kevin
    Dec 1, 2017 at 12:25
  • 1
    He's on Ubuntu but it was just a thought. @Clive see: github.com/acquia/blt/issues/2251#issuecomment-342665375 using a certain version of git and OSX can cause patches to not apply in some cases.
    – Kevin
    Dec 1, 2017 at 15:05
  • 1
    Wow, that's some edge case @Kevin. Cheers for the link
    – Clive
    Dec 1, 2017 at 15:08
  • 1
    Yep, I ran right into it. Was tough to narrow down. Composer Patches was recently updated, as was Composer.
    – Kevin
    Dec 1, 2017 at 21:37

3 Answers 3

15

First, run

composer require cweagans/composer-patches

Now you have a package installed that enables you to patch core, libraries or modules.

Second, add a patch to a folder named 'patches' in the root (where your composer.json file is).

Third, add an "extras" section in composer with a patch:

"extra": {
    "patches": {
        "drupal/core": {
            "Undocumented title variable in feed-icon.html.twig": "patches/3156260-11.patch"
        }
    }
}

OR

"extra": {
    "patches-file": "composer.patches.json"
}

with a composer.patches.json file that contains patches like this:

{
    "patches": {
        "drupal/core": {
            "Undocumented title variable in feed-icon.html.twig": "patches/3156260-11.patch"
        }
    }
}

Fourth, run

composer install

This will remove the core, re-download and patch it. Same goes for modules and libraries.

Side note: you can also use an external url of the patch instead of a local file in your patches folder.

Source: section 1.1.3 of https://stefvanlooveren.me/courses/drupal-9-framework#toc

1
  • 2
    This is the best way to do it as it works with modules that are installed normally with composer. Make sure you have the cweagans composer-patches and the patches config in extra will work. You can use the patch url from drupal.org as well, but those can be removed.
    – mortona42
    Nov 28, 2020 at 17:56
10

The issue is that composer is applying patches using git apply which is not working for packages that are not installed from source.

Therefore the solution is to install the packages from source. This way the package will fetch the package along with its repository (when available) thus enabling it to be patched.

So...

How to install/update a package from source

For new packages:

Use

composer require vendor/package --prefer-source

For existing packages

If your package is already installed then you need to delete it so that you can re-fetch it with composer install (there is no composer reinstall command). Do so and then use

composer install vendor/package --prefer-source

Set it as the default behavior

You can also set it up as the default behavior for the project in the config section of composer.json:

"config": {
      "preferred-install": "source"
    },

Update:

Set it as the default behavior for a specific package

In case you don't want to set your entire project to install from source, you can do so for a specific package only. To do so you need to specify the VCS Branch in the Version constrain of the package.

"require": {
        "vendor/package": "dev-branch#branch-hash"
}
9

To apply a patch in drupal 8, edit the composer.json file and find the extra section -

"extra": {
    .
    .
    .
    "patches": {
        "drupal/MODULE_NAME": {
            "ANY_STRING_TO_NAME_THE_APPLYING_PATCH": "PATCH URL"
        }
    },
    "enable-patching": true
}

For Example -

"extra": {
    "installer-types": [
        "bower-asset",
        "npm-asset"
    ],
    "patches": {
        "drupal/recaptcha": {
            "Drupal recaptcha ajax fix syncing": "https://www.drupal.org/files/issues/2019-04-17/recaptcha-ajax-2493183-172.patch"
        }
    },
    "enable-patching": true
}

Then, run composer install and your patch is applied.

If you want to apply multiple patches for same package, add new key inside the package name

Example :-

"patches": {
   "drupal/MODULE_NAME": {
          "Patch name 1": "Patch 1 Url", 
          "Patch name 2": "Patch 2 Url" 
        }
    },
2
  • 1
    Why is the "enable-patching": true. Is this necessary? Oct 20, 2020 at 15:09
  • @StefVanLooveren You should use it if you want your project to accept patches from dependencies: github.com/cweagans/…
    – rpayanm
    Apr 7, 2021 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.