Applying Magento 2 Github Patches

There are times when Magento will add fixes to their code base via Github without adding them straight away to newly released versions of the framework. This is where applying Magento 2 Github patches can be useful to patch any bugs in the code without having to potentially wait weeks for Magento to release a new version.

Recently we came across an issue with the wysiwyg editor’s ‘Show/Hide’ button not always working within the admin area when editing content on CMS pages. This made it difficult for the merchant to edit their content as only the HTML source code was visible.

The patch, located here: https://github.com/magento/magento2/commit/3524943ac6544ee969480536ef6f0cd1a29b0b56.patch can be applied if you install the cweagans/composer-patches package via composer.

So head to your root directory of your Magento 2 project, and run the composer require command to install the package.

$ composer require cweagans/composer-patches

Now check your project’s composer.json file and you should see the package added under the require package link.

"require": {
    "magento/product-community-edition": "2.1.7",
    "composer/composer": "@alpha",
    "cweagans/composer-patches": "^1.6"
},

You’ll now be able to apply a patch from a local or remote file.

Add the following within your composer.json‘s extra package link.

"extra": {
    "patches": {
        "magento/magento2-base": {
            "[PATCH] MAGETWO-57675: [GITHUB] WYSIWYG editor does not show. #6222": "vendor_patches/MAGETWO-57675-Base.diff"
        }
    }
}

Create a vendor_patches directory and the MAGETWO-57675-Base.diff file, and paste the contents of the Github .patch file into this file.

Now run the composer update command, and you should see the patch being applied in the Terminal output.

$ composer update

...

  - Applying patches for magento/magento2-base
    vendor_patches/MAGETWO-57675-Base.diff ([PATCH] MAGETWO-57675: [GITHUB] WYSIWYG editor does not show. #6222)

The file in question should also be patched.

// lib/web/mage/adminhtml/wysiwyg/tiny_mce/setup.js

        ...

        setup: function(mode) {
            if (this.config.widget_plugin_src) {
                tinyMCE.PluginManager.load('magentowidget', this.config.widget_plugin_src);
            }

            if (this.config.plugins) {
                this.config.plugins.each(function(plugin) {
                    tinyMCE.PluginManager.load(plugin.name, plugin.src);
                });
            }

            if (jQuery.isReady) {
                tinyMCE.dom.Event.domLoaded = true;
            }

            tinyMCE.init(this.getSettings(mode));
        },

        ....
    };
});

Rather than keeping a list of patches within composer.json, you can also add them to an external JSON file by using the patches-file key.

"extra": {
    "patches-file": "local/path/to/your/composer.patches.json"
}

Then within composer.patches.json, specify the patch as previously specified within composer.json.

{
  "patches": {
    "magento/magento2-base": {
        "[PATCH] MAGETWO-57675: [GITHUB] WYSIWYG editor does not show. #6222": "vendor_patches/MAGETWO-57675-Base.diff"
    }   
  }
}

Note: This article is based on Magento CE version 2.1.