As always-evolving Drupal developers, we have been in the process of moving towards having a Composer-based workflow for managing our Drupal project codebases. While it is (normally) an easy jump from "drush dl" to "composer require" for Drupal contrib modules and themes, there's another significant opportunity for us to take advantage of during this evolution that should be considered.
We are all familiar with the concept that Drupal modules extend the functionality of Drupal site; we should also be embracing the fact that there is a whole class of Composer dependencies that extend Composer's functionality. These are appropriately called "Composer plugins".
Composer Plugins 101
If your Drupal project is using the drupal/recommended-project or drupal-composer/drupal-project Composer template (or similar), then you're already using Composer plugins. The second part of this article will surface some additional Composer plugins that you may want to consider adding to your Drupal codebase.
Adding a new Composer plugin is normally a two-step process. First, use "composer require" to add the plugin to your project. The second step usually (but not always) involves adding some plugin configuration to your project composer.json's "extra" section.
For example, one of the dependencies of the drupal/core-recommended Composer template is the composer/installers plugin - this is how Composer knows to put Drupal modules in the modules/contrib/ directory and not in the (Composer default) vendor/ directory.
If you were working on a project that didn't already use composer/installers, then you would need to add it to the project using
composer require composer/installers
Then, after reading a little bit of documentation, you would learn that in order for Composer to place dependencies of type "drupal-module" (as defined in the module's composer.json file) in your project's web/modules/contrib/ directory, you would need to add the following to your project composer.json's "extra" section:
"extra": { "installer-paths": { "web/modules/contrib/{$name}": ["type:drupal-module"], } }
One other thing to be aware of when it comes to Composer plugins is that many (most?) plugins are able to run directly via a Composer command. For example, the drupal/core-composer-scaffold command (details below) can be run at any time via "composer drupal:scaffold".
Side note: if I have one complaint about Composer plugins it is that often the plugin's vendor/name are not consistent with the plugin's defined key in the "extra" section (composer/installers vs. installer-paths). I would much prefer if the key was defined based on the plugin's vendor/name. For example, something like "composer-installers-paths" - this would make it easier for new users to recognize the relationship between plugins and data in the "extra" section, IMHO.
Useful Composer Plugins for Drupal projects
Must haves
- Description: Allows dependencies to be placed in directories other than /vendor/.
- Availability: Included as part of drupal/recommended-project Composer template (via drupal/core-recommended), part of the default drupal-composer/drupal-project template.
- Configuration key: installer-paths
- Description: Places default Drupal scaffolding files outside of the /core/ directory and allows for modifications to scaffolding files.
- Availability: Included as part of drupal/recommended-project Composer template, part of the default drupal-composer/drupal-project template.
- Configuration key: drupal-scaffold
- Description: Automatically applies patches (both local and remote) to dependencies.
- Availability: Included as part of the default drupal-composer/drupal-project template.
- Configuration keys: patches, patches-file, enable-patching, patches-ignore.
Worth considering
zaporylie/composer-drupal-optimizations
- Description: Provides Composer performance boost for Drupal projects by ignoring legacy symfony tags.
- Availability: Included as part of the default drupal-composer/drupal-project template.
- Configuration keys: composer-drupal-optimizations.
topfloor/composer-cleanup-vcs-dirs
- Description: Automatically removes .git directories for cloned dependencies. Only necessary when dependencies are committed to the project's Git repository.
- Availability: Install as you would any other Composer dependency.
- Configuration keys: None.
- Description: Companion plugin to cweagans/composer-patches that allows for the addition of patches (and other functionality) from the command line.
- Availability: Install as you would any other Composer dependency.
- Configuration keys: None.
oomphinc/composer-installers-extender
- Description: Companion plugin to composer/installers that allows for any arbitrary package type (such as npm packages) to be defined and then handled by the composer/installers plugin.
- Availability: Install as you would any other Composer dependency.
- Configuration keys: installer-types.
- Description: Creates a simple "composer-manifest.yaml" file in the project root listing all dependencies (and their exact version numbers) currently used in a project.
- Availability: Install as you would any other Composer dependency.
- Configuration keys: None.
- Description: Composer performance boost - enables parallel downloads of dependencies during Composer commands. Will not be necessary in Composer 2.x.
- Availability: Install as you would any other Composer dependency.
- Configuration keys: None.
Not necessary for experienced developers
- Description: Allows text to be displayed after "composer create" and "composer install" commands have completed.
- Availability: Included as part of drupal/recommended-project Composer template.
- Configuration key: drupal-core-project-message
Am I missing any? If so, please let me know in the comments below. Thanks to everyone who responded to my Twitter post on this topic!
Want to learn more about Composer? Check out our Composer Basics for Drupal Developers workshop.