Drupal 10
As reported by @rocketeerbkw, the official lenient endpoint in packagist currently doesn't support Drupal 10.
https://github.com/mglaman/composer-drupal-lenient does.
Instructions for use are in the repo.
Drupal 9
The official advice is now to use a specially made lenient endpoint as described in Using Drupal's Lenient Composer Endpoint.
Issue forks
Official advice found at: Use issue forks to make compatibility fixes work with Composer
Copied here for convenience:
It's impossible to install an incompatible module with composer and apply a compatibility patch afterwards. However, since issue forks are branches it's possible to install the module using that branch. In order to do this we need to do 3 things.
Under the repositories section where the composer source is listed we need to add an exclude key for our module that we're trying to install using the issue fork. In my example I'm trying to install a issue for the Homebox module.
{
"type": "composer",
"url": "https://packages.drupal.org/8",
"exclude": [
"drupal/homebox"
]
}
Next up we need to add the specific git URL to our repositories section. You can find this URL under the "show commands" section of the issue fork.
{
"type": "git",
"url": "[email protected]:issue/homebox-3146462.git"
}
Our complete repositories key looks something like this now:
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8",
"exclude": [
"drupal/homebox"
]
},
{
"type": "git",
"url": "[email protected]:issue/homebox-3146462.git"
}
],
Now we can use the composer install command to require the module. Important here is that you need to specify the branch name prefixed with "dev-". So for the homebox example the command would look like this:
composer require 'drupal/homebox:dev-3146462-drupal-9-compatibility'
It's possible that for the first time when you execute this you're prompted to allow the ECDSA key fingerprint for git.drupal.org. Just type yes and composer will continue.
Commit reference
You've got the right idea but you don't need to fork Drupal's repository. You can set up composer to read Drupal's git repo instead of using packagist for the naughty modules like this:
{
"type": "package",
"package": {
"name": "drupal/cache_control_override",
"type": "drupal-module",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://git.drupalcode.org/project/cache_control_override.git",
"reference": "8db91684a427366d8f9c51f60cbac10c2d586d95"
}
},
{
"type": "composer",
"url": "https://packages.drupal.org/8"
}
},
Note the reference
is a commit hash, though it looks like you can also use tags. Also note that the custom git type: package
repo must be before Drupal's default type: composer
repo in your composer file.
And then patch as normal using composer:
"drupal/cache_control_override": {
"Drupal 9 Compatibility (3132036)": "https://www.drupal.org/files/issues/2020-04-29/Drupal-9-readiness-3132036-2.patch"
},
Thanks @miststudent2011 for pointing out that this doesn't automatically install dependencies which I can confirm. You will need to require those yourself!
(Shamelessly stolen from @acbramley on Drupal Slack because wider dissemination of this knowledge is worthwhile)
drupal/core:^8.something
. The project is set up withdrupal/core:^9.0.6
core_version_requirement
in the module's .info.yml which informs the packager of the core dependency. If that's not available, it's presumed to be just 8 (or it's derived from thecore
key instead perhaps)