Drupal Composer Recipes
- Transfer
In this post we want to share some recipes for using Composer that we have accumulated while working with Drupal projects created using the Drupal Composer template. We will also look at how to transfer an existing Drupal project to Composer. If you are still not using Composer in Drupal projects, you should start doing it right away! Drupal Composer template will help to cope with this task. Creating a new project is very simple.
If you're still unsure, take a look at the benefits of Drupal Composer development:
- There is no need to store the code of the plugin modules (and the kernel itself!) In your version control system.
- A single package management tool for everything: the Drupal core, contrib modules, JS libraries, your own modules used in different projects, etc.
- The most simple and convenient patch of the kernel and modules.
- It is much easier to use Git submodules .
(All recipes imply the use of Drupal 8, but they should also work for Drupal 7)
Installing the plugin modules
composer require drupal/to install the latest stable release (or the latest dev version, if there are no releases for Drupal 8 yet):~8.0 composer require drupal/to install the latest dev version:dev- composer require drupal/to install a specific version:dev- #
Updating Drupal Kernel and Modules
composer updateto update everythingcomposer update --dry-runto check for updatescomposer update drupal/to update a specific module
Patch package
The cweagans / composer-patches plugin (included with the Drupal Composer template) uses the patches described in the extra section of composer.json:
"extra": {
"patches": {
"": {
"": "",
...
},
...
}
} Example:
"extra": {
"patches": {
"drupal/core": {
"Fix language detection": "patches/2189267-24.patch"
}
}
}After the patch is added, run:
composer installto apply the patchcomposer update nothing(orcomposer update --lock) for the composer-patches plugin to make the necessary changes in the composer.lock file
Install custom / forked modules with Github
If the module repository contains its own composer.json file
Register the repository in the “repositories” section of the composer.json file:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/"
},
...
],
Use to install the module.
composer require drupal/:dev-# If the composer.json file is not in the module repository
Use a slightly more advanced option:
"repositories": [
{
"type": "package",
"package": {
"name": "drupal/",
"version": "dev-custom",
"type": "drupal-module",
"source": {
"type": "git",
"url": "git@github.com:.git",
"reference": ""
}
}
},
...
],
Use to install the module.
composer require drupal/:dev-custom# If the destination directory should be different from modules / contrib
In addition to the above recipes, use the composer / installers plugin:
"extra": {
"installer-paths": {
"web/modules/custom/": ["drupal/"],
...
}
} Install JS Library
Popular JS libraries can be easily installed using Composer, as they (most likely) already exist in the Packagist repository . The difficulty is that most Drupal modules require installing JS libraries in the “libraries” directory, while Composer installs them in the “vendor” directory.
The composer / installers plugin can reassign the installation path, but only for those packages that indicate it as a dependency. Thus, you need to replace the composer.json library file, indicating in it the dependence on composer / installers.
Take a look at an example:
"repositories": [
{
"type": "package",
"package": {
"name": "enyo/dropzone",
"version": "4.3",
"type": "drupal-library",
"source": {
"url": "https://github.com/enyo/dropzone.git",
"type": "git",
"reference": "master"
},
"dist": {
"url": "https://github.com/enyo/dropzone/archive/v4.3.0.zip",
"type": "zip"
},
"require": {
"composer/installers": "~1.0"
}
}
},
...
],
...
"extra": {
"installer-paths": {
"web/libraries/{$name}" : ["type:drupal-library"],
...
}
}After this code is added to composer.json, run
composer require enyo/dropzone:4.3to install the library. Notice that we specified a specific version and added the “dist” section so that Composer could load the zip archive instead of cloning the repository.Switch an existing package to a forked version
Register the fork repository in composer.json:
"repositories": [
{
"type": "vcs",
"url": "https://github.com/"
},
...
], Run
composer require :dev-# Switch existing Drupal 8 project to Composer
- Make backup;)
- Delete everything Composer will manage: the “core” Drupal directory, the plugin modules, etc.
- Delete all root Drupal files, such as index.php, update.php, README.txt ... All of them.
- Create a “web” directory in the root of the project and move there all the remaining Drupal directories (sites, modules, themes, libraries, profiles, etc.)
- Copy the Drupal Composer template files to the root of the project.
- Make a list of versions of the plug-ins used in the project, the Drupal kernel, and everything else Composer will manage. Then run by
composer requirespecifying a specific version for each dependency. You will need to translate the Drupal version into the Composer version, here are a few examples:drupal/core:8.1.8it all fits togetherdrupal/admin_toolbar:8.1.15implies admin_toolbar 8.x-1.15drupal/ctools:8.3.0-alpha26implies ctools 8.x-3.0-alpha26drupal/config_installer:dev-8.x-1.x#a16cc9acf84dd12b9714def53be0ce280a5b0c1aimplies a dev version of config_installer created from a16cc9a commit brunch 8.x-1.x
- In the “require” section of composer.json, change the versions of the Drupal kernel and the plug-in modules to “~ 8.0”. This will make future updates possible.
- Run
composer drupal-scaffold, this will create the necessary root Drupal files. - Make sure your web server uses the web directory as web root.