Apply Drupal 8 Patch by Composer without update Drupal Core

Events happening in the community are now at Drupal community events on www.drupal.org.
vickey's picture

Use the below procedure to apply the patch for composer based Drupal 8 website without updating the core version.

FYI, There was high critical security vulnerability issue fix :
https://www.drupal.org/sa-core-2018-002
https://groups.drupal.org/security/faq-2018-002

Please make sure "cweagans/composer-patches" plugin added in your composer.json and lock file or else use below command to add them.
composer require cweagans/composer-patches

Steps to update the patch.

1) Please edit the composer.json file and add below line and end of the file like below format.

"extra": {
    "enable-patching": true,
    "patches": {
        "drupal/core": {
            "<patch1 information>": "<patch1 file path>",
            "<patch2 information>": "<patch2 file path>"
        }
     }
}

2) composer install

After composer install, some time composer.lock file might not updated with latest patch information so please use below command(step-3).

3) composer update --lock

This will generate composer.lock file from composer.json with latest changes like patch changes. make the composer files are up to date.

Same steps used to apply patch for module only add the patch information in respective module block under "patches" section like below.

"patches": {
   "drupal/module_name": {
      "<patch information>": "<patch file path>"
    }
}

Comments

Thanks

bhushan.nagaonkar's picture

Thanks exactly what I was looking for.

How to apply for library under vendor folder

selvakumar's picture

I need to apply patch for library under vendor folder. For example:
cd vendor/solarium/solarium

wget https://patch-diff.githubusercontent.com/raw/solariumphp/solarium/pull/5...

patch -p1 <519.patch

How to do this?

Selvakumar Thangavelu
Drupal Lead

Please check below comment

vickey's picture

Please check below comment for the answer.

To apply patch for any composer based package

vickey's picture

Please add your composer package name("solarium/solarium") under "patches" section like below.

"patches": {
   "solarium/solarium": {
      "<patch information>": "<patch file path>"
    }
}

It will works.

Please tell me what to do after applying the patch.

s-jack's picture

How do I describe it in Composer.json and apply the patch?
Should I delete the description manually?

Linked to this page from:Set

clarification

miedward's picture

From the cweagans README.md

{
  "require": {
    "cweagans/composer-patches": "~1.0",
    "drupal/drupal": "~8.2"
  },
  "config": {
    "preferred-install": "source"
  },
  "extra": {
    "patches": {
      "drupal/drupal": {
        "Add startup configuration for PHP server": "https://www.drupal.org/files/issues/add_a_startup-1543858-30.patch"
      }
    }
  }

It is not at all clear what should be in <patch file path> from the original post. I assumed it was a file path and wasted a lot of time before I looked at the project README.
thirstysix's picture

I tried the above steps. but, no luck.

Drupal version - 8.9
Added composer.json
"enable-patching": true,
"patches": {
"drupal/core-recommended": {
"Allow text field to enforce a specific text format": "https://www.drupal.org/files/issues/2020-03-15/784672-164.patch"
}
}

composer install
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2020-03-15/784672-164.patch

[ErrorException]
file_put_contents(/PATCHES.txt): failed to open stream: Permission denied

Fix failed core patch due to permission denied

wranvaud's picture

@ThirstySix on your composer.json file instead of applying the patch to "core-recommended" you have to set it to "core" like this:

"patches": {
  "drupal/core": {
    "784672-164": "https://www.drupal.org/files/issues/2020-03-15/784672-164.patch"
  }
}

Yes. Already,I fixed it.

thirstysix's picture

Yes. Already,I fixed it. Thank you @Wranvaud.

Parse error or Nothing to install or update

manoloka's picture

Hi there,

I tried the applying a patch using the instructions above but I get a "Parse error" or a "Nothing to install or update".

Obviously I'm doing something wrong but I cannot figure out what that is.

I added the code below right under the "extras"

"patches": {
   "drupal/module_filter": {
      "patch module filter perms filter": "https://www.drupal.org/files/issues/2020-07-27/3024306-7.module_filter.EnableDisable-Permissions-Filter-Avoids-conflicts-Patch-Inside.patch"
    }
}

and that gives me this error

  [Seld\JsonLint\ParsingException]
  "./composer.json" does not contain valid JSON
  Parse error on line 39:
        "drupal-scaffold": {
  -------------------^
  Expected one of: 'EOF', '}', ',', ']'

so I added a comma after the last brace

"patches": {
   "drupal/module_filter": {
      "patch module filter perms filter": "https://www.drupal.org/files/issues/2020-07-27/3024306-7.module_filter.EnableDisable-Permissions-Filter-Avoids-conflicts-Patch-Inside.patch"
    }
},

and then I get a "Nothing to install or update" message

What am I missing?
I'm new to composer so I'm trying to learn

Here you can see my whole composer.json

{
    "name": "drupal/recommended-project",
    "description": "Project template for Drupal 8 projects with a relocated document root",
    "type": "project",
    "license": "GPL-2.0-or-later",
    "homepage": "https://www.drupal.org/project/drupal",
    "support": {
        "docs": "https://www.drupal.org/docs/user_guide/en/index.html",
        "chat": "https://www.drupal.org/node/314178"
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        }
    ],
    "require": {
        "composer/installers": "^1.2",
        "drupal/admin_toolbar": "^2.3",
        "drupal/core-composer-scaffold": "^8.8",
        "drupal/core-project-message": "^8.8",
        "drupal/core-recommended": "^8.8",
        "drupal/filter_perms": "^1.0@alpha",
        "drupal/module_filter": "^3.1"
    },
    "conflict": {
        "drupal/drupal": "*"
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "config": {
        "sort-packages": true
    },
    "extra": {
"patches": {
   "drupal/module_filter": {
      "patch module filter perms filter": "https://www.drupal.org/files/issues/2020-07-27/3024306-7.module_filter.EnableDisable-Permissions-Filter-Avoids-conflicts-Patch-Inside.patch"
    }
},
        "drupal-scaffold": {
            "locations": {
                "web-root": "web/"
            }
        },
        "installer-paths": {
            "web/core": [
                "type:drupal-core"
            ],
            "web/libraries/{$name}": [
                "type:drupal-library"
            ],
            "web/modules/contrib/{$name}": [
                "type:drupal-module"
            ],
            "web/profiles/contrib/{$name}": [
                "type:drupal-profile"
            ],
            "web/themes/contrib/{$name}": [
                "type:drupal-theme"
            ],
            "drush/Commands/contrib/{$name}": [
                "type:drupal-drush"
            ],
            "web/modules/custom/{$name}": [
                "type:drupal-custom-module"
            ],
            "web/themes/custom/{$name}": [
                "type:drupal-custom-theme"
            ]
        },
        "drupal-core-project-message": {
            "include-keys": [
                "homepage",
                "support"
            ],
            "post-create-project-cmd-message": [
                "<bg=blue;fg=white>                                                         </>",
                "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",
                "<bg=blue;fg=white>  from the drupal/recommended-project template!          </>",
                "<bg=blue;fg=white>                                                         </>",
                "",
                "<bg=yellow;fg=black>Next steps</>:",
                "  * Install the site: https://www.drupal.org/docs/8/install",
                "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",
                "  * Get support: https://www.drupal.org/support",
                "  * Get involved with the Drupal community:",
                "      https://www.drupal.org/getting-involved",
                "  * Remove the plugin that prints this message:",
                "      composer remove drupal/core-project-message"
            ]
        }
    }
}

It's a patch to the module filter that affects the permissions filter. I've done many attempts and if permissions filter is disabled, the composer update resulted in a

Removing drupal/filter_perms (1.0.0-alpha1)

I'm completely lost

Thanks for your help

manoloka it looks like you

miedward's picture

manoloka it looks like you missed this step, based on your composer.json. it is correct with the comma but it doesn't actually do anything with the patch info without the package being installed also.

composer require cweagans/composer-patches

OMG how could I miss that

manoloka's picture

OMG how could I miss that :(

thanks for pointing it for me and sorry for my silliness
Manu

That got composer try to

manoloka's picture

That got composer try to attempt to apply the patch, however I got another error message "Could not apply patch! Skipping. The error was: Cannot apply patch "

I've posted a comment at the issue where the patch comes from https://www.drupal.org/project/module_filter/issues/3024306#comment-1383...

miedward thanks for your help

The patcher is not very smart

miedward's picture

The patcher is not very smart and if the code base changes too much the patch will have to be updated or it can fail like that. Hopefully someone can revise the patch for you so it works.

works like a charm. Thanks

usmanjutt84's picture

works like a charm. Thanks

I'm trying to update the

bala_28's picture

I'm trying to update the version of external lib through this patch.
"patches": {
"drupal/password_strength": {
"Not working with newer versions of bjeavons/zxcvbn-php[https://www.drupal.org/project/password_strength/issues/3156179]": "https://www.drupal.org/files/issues/2022-02-18/password_strength_php8.patch"
}
},

Patch is applied on composer install. But the library is not upgraded even when I run the composer update command as described above.
Am I missing anything here?

Composer patching core resets permissions

Youcanlearnit's picture

Hi,

When running composer install, it patches drupal core but changes the
web/core folder permissions from 750 to 644
Is this normal? I have to manually change web/core folder permissions to 750 or 755 to get the site back working.

Before I run composer install, I pull changes git pull --ff-only

What am I doing wrong, why the core folder permissions are reset everytime?

michelrrr's picture

Hello, I'm using composer 2 with drupal 10.
when I apply the proposed update composer command line, it keeps doing the following :
- uninstall drupal/core
- install drupal/core
- apply patches

Question is : How to avoid the uninstall and install steps ??
I just need to apply a patch and not reinstalling the full core.

thanks
Michel

Drupalians

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: