How to add a fork to a composer Project

There are occasions when a project is needed that isn’t yet ready for Drupal 8 or 9. Hopefully, it is only missing a couple of configuration options like the the new core_version_requirement key in *.info.yml, but sometimes it needs more work.

The obvious next step would be to create a patch or a pull request and then help to get the fix implemented in the issue queue on Drupal.org, but while waiting for the review you may need to get that update into your own composer.json file.
This could prove tricky as you can sometimes find yourself in a chicken and egg scenario where you need to patch the module before it can be added to a project, but it needs to be in the project so you can patch it.

This is where a personal fork of the module can help while waiting for the patch to be accepted and committed to the original project.

Looking at the docs: https://getcomposer.org/doc/05-repositories.md#vcs

We can add a personal fork as a repository in the composer.json file and then reference the custom branch in the require statement.

As an example:

SeveralDrupal 9 projects are being develop locally using ddev and Acquias BLT. It is helpful to integrate BLT and ddev, but the project that does that (https://github.com/lcatlett/blt-ddev/) isn’t quite ready for Drupal 9.

The first step in solving this is to look at the issue queue and the open pull requests and while this issue has been solved in some Pull Requests, the solutions have not yet been accepted into the project.

There are also a couple of other improvements that could be made to those PRs as well, so I decided to create my own fork of the project at: https://github.com/thegbomb/blt-ddev

In order to add this to a project a new branch is needed so that it can be referenced, so I created d9-branch and added my own fixes to that. [edit: It seems that a new branch may not have been needed and referncing dev-master may be enough to tell composer to use the fork, but I haven't tested that]

Now to tell composer about the new repo, so find the repositories key in the project composer.json file and add:

    {
        "type": "vcs",
        "url": "https://github.com/thegbomb/blt-ddev"
    }

There may already be a composer reference there so just add the new reference after it, which will end up looking like:

"repositories": [
    {
          "type": "composer",
          "url": "https://packages.drupal.org/8"
    },
    {
        "type": "vcs",
        "url": "https://github.com/thegbomb/blt-ddev"
    }
],

Next the require statement needs to be changed. The original project should still be required, not the new fork, but the version constraint will point to the new branch. This is done by adding dev- to the branch name e.g.

composer require lcatlett/blt-ddev:dev-d9-branch

That should now update and bring in the new forked version of that project.

Add new comment