Tutorial

How to apply patches with Composer

Ever wondered how to have a stable installation of Drupal 8 with a lot of modules and in need of applying a patch? If you apply it manually, composer will overwrite your patch later when updating a module. Follow these simple steps.

Pridal/a lubo dňa Št, 02/27/2020 - 15:34
Install your Drupal 8 with Composer

Do not forget, that you need to have Composer installed in your Drupal installation from the beginning. Use the following command to install Drupal 8 with Composer.

$ composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interact

You can easily follow steps in this tutorial as well:

https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies

Do not forget, that you have to have Composer installed in your operating system in order to make things work properly.

Apply the patch in composer.json file

Assuming you have already installed Drupal and used Composer to install new modules, you just need to add those lines to your composer.json file located in Drupal root directory.

    "extra": {
        "drupal-scaffold": {
            "locations": {
                "web-root": "./"
            }
        },
        "installer-paths": {
            "core": ["type:drupal-core"],
            "libraries/{$name}": ["type:drupal-library"],
            "modules/contrib/{$name}": ["type:drupal-module"],
            "profiles/contrib/{$name}": ["type:drupal-profile"],
            "themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/Commands/contrib/{$name}": ["type:drupal-drush"],
            "modules/custom/{$name}": ["type:drupal-custom-module"],
            "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/legacy-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"
            ]
        },
        "patches": {
            "drupal/views_fieldsets": {
                "Drupal Views Fieldsets patch": "https://www.drupal.org/files/issues/2018-05-30/views_fieldsets-fix_empty_handler_query-2908454-56.patch"     
            }
        }

Please notice the last section "patches", where you can easily put as many patches as needed.

Run Composer Install or Composer Update

Now all you need to do is run Composer install or Composer update command and patch will be applied accordingly.

Still not installing patches properly?

If you happen to struggle with installing patches, try to run this command:

composer require 'cweagans/composer-patches'

Which should allow you to install patches afterwards. This might come handy if your Drupal was not installed by composer in the first place but you want to "composerize" your Drupal installation afterwards.

Might interest you

Tutorial
So you need to push a new variable to TWIG file and write it out somewhere? You can do so by following code. It is universal and fits…
Tutorial
I had trouble to set this up properly, because documentation is quite misleading or often not there at all. Basicly nowdays (2022) it is…

Recommended

Article
32 views
For the past few days I am trying to comprehend why / how this blockchain even gained it'…
Tutorial
95 views
This sketch is quite easy, I used Arduino Nano with OLED 0.96″ display 128×64 resolution…
Tutorial
157 views
While working on a fairly complex website with very complex views setup, including tens…
Tutorial
14 views
In this case we have two options, either we use hook_user_presave() or we can create new…
Tutorial
15 views
When using Swiftmailer under Drupal 8 / 9 it automatically sets the headers for sender to…
Tutorial
7 views
Yes, IOS / Safari is the new internet explorer. Amount of time I spend on debugging…
Tutorial
43 views
There is a very handy function in Drupal 8 / 9, allowing developers refresh view when…
Tutorial
22 views
Often, when doing SEO checkups, SEO specialist come up with adding Schema.org…
Tutorial
175 views
I needed to test my contracts against USDC contract, specifically I needed ERC-721 mint…
Tutorial
85 views
If you are a newbie like I am and struggling with setting the proper MYSQL my.cnf config…
Tutorial
25 views
I had trouble to set this up properly, because documentation is quite misleading or often…
Article
72 views
As the title says, DO NOT in any circumstances install ANY bitcoin price extension to ANY…
Tutorial
278 views
This is (or should be) a working example of sending some Ether between two addresses.…
Module
45 views
This list was fetched from Zapper, with their /v1/token-list endpoint. Which you can…
Tutorial
143 views
In the last months I am being pretty much bombarded by my clients with asking what…