Skip to main content

Drupal 8-9: How to apply a Drupal / contrib patch with the cweagans/composer-patches package

Applying patches in drupal ^8 is different compared to drupal 7.

Composer does a decent job alleviating the headaches of managing several sites with different dependencies. However, out of the box Composer will revert patched core files and contrib modules and it is for that reason composer-patches project was created. With this post, we are going to review how to set up composer-patches for a drupal composer managed project (the default and mandatory way of configuring and mainting a new drupal instance since drupal 8) and how to specify local or remote hosted patches. [from https://www.mediacurrent.com/blog/composer-patches-dependency-your-proj…]

If you have not yet installed then you should first install the cweagans/composer-patches package (https://github.com/cweagans/composer-patches) via composer composer require cweagans/composer-patches:~1.0 --update-with-dependencies

Now after this you can open your /root/composer.json file and add this section within the extra section like this

"extra": {
        "patches": {
            "drupal/core": {
                "View date filter": "https://www.drupal.org/files/issues/2021-04-20/2647292-57-datetime_viewsfilter_strtotime.patch",
                "Date type filter": "https://www.drupal.org/files/issues/2020-12-23/3189302-7.patch"
            },
            "drupal/radix" : {
                "Notice in radix_preprocess_form_element" : "https://www.drupal.org/files/issues/2021-09-16/3233510-fix-notice.patch"
            },
           "drupal/entity_browser": {
                "Entity browser - PDP": "https://www.drupal.org/files/issues/2021-07-12/entity-reference-missing-product-remove-fix.patch"
            },
            "drupal/multivaluelfield": {
                "Undefined index: data": "https://www.drupal.org/files/issues/2021-02-25/undefined-index-data-3179752-6.patch"
            }
        },

The format for each patch is : “Patch description”: “path to patch file”

Run composer validate in order to make sure your json format is valid, and then run composer update

If everything is ok you should see the applying patches for drupal/core message with success.

The first two patches are as you can guess patches for the drupal/core and the second is a patch for the radix theming engine. The rest (3rd and 4th) are patches for the contrib modules of entity_browser and multivaluefield.

Notice that the drupal core has 2 patches and both of them are included in the same patching "section" of the composer json file. When we have 2 patches of the same module or core or theme we don't create a separate entry for each patch but we group them together in the same "section".

Good Luck!

patches patch core composer