How to modify the concrete5 core with Composer patches

Sometimes you want to change something in concrete5, but it can't be done easily via a package or with some custom code. What can we do in such a case? Well, of course we don't want to modify the core concrete5 files. Unless...?

Yes, unless there is a proper way to do so of course! I came across a solution watching the concrete5 Github repository where mlocati posted an interesting approach I had not heard of before. Important, you need a Composer based installation of concrete5 for this approach.
 

How does it work?

Here's how you can apply a patch to the concrete5 core:

  1. Fork the concrete5 repository, if you haven't yet.
  2. Commit your changes to the core in your local fork.
  3. Create a patch file:
     
# The -1 is the number of commits that should be included in the patch file.
git format-patch --stdout -1 > your-patch-file.patch

 

  1. Copy the file to your repository, e.g. to 'patches/your-patch-file.patch'.
  2. Add this section to your composer.json file:
     
"patches": { 
    "concrete5/core": { 
        "A descriptive title of your patch": "patches/your-patch-file.patch"
    }
}

 

  1. Make sure you add cweagans/composer-patches to your Composer dependencies via:
     
composer require "cweagans/composer-patches:~1.0"
 

When do I need this approach?

It can be handy in case:

  • concrete5 hasn't merged your pull request, but you really need the change.
  • a certain file or class can't be overridden easily.
  • you need a small tweak / customization in the core, but you don't want to maintain a fork, and you don't think concrete5 will merge a pull request with the change.
     

Need help?

Make sure you join Slack via https://slack.concrete5.org and ask your question in the #dev channel.