Managing Drupal with Composer

DrupalTM Blog

Welcome to our Drupal blog! Stay up to date with the latest Drupal news, best practices, and techniques.

 

Composer is a tool for managing dependencies in PHP projects, and it is essential for managing Drupal and its dependencies, including modules, themes, and libraries. Ensure Composer is installed on your local machine before executing any Composer commands.

Installing Drupal

For Drupal 10:

Use the following command to create a new Drupal project

composer create-project drupal/recommended-project my_site_name

For Drupal 7:

composer create-project drupal-composer/drupal-project:7.x-dev -n my_site_name

This will create a project in the my_site_name directory, automatically downloading the latest stable version of Drupal and its dependencies. The web root will be the my_site_name/web directory.

Specifying a Version:

To download a specific version of Drupal, append the version number:

composer create-project drupal/recommended-project:9.3.12 my_site_name

For the latest version of a specific release series:

composer create-project drupal/recommended-project:^9.3 my_site_name

Using Docker:

If using a Docker-based workflow:

docker run --rm -i --tty -v $PWD:/app composer create-project drupal/recommended-project my_site_name --ignore-platform-reqs

Installing Drupal

Via Web Interface:

After downloading, navigate to your site's URL to start the setup process, which includes entering database credentials and setting up an admin user.

Via Command Line:

Add Drush to your project:

composer require drush/drush

Install Drupal using Drush:

drush site:install

Quickstart Feature:

For a quick demo site:

php ./web/core/scripts/drupal quick-start demo_umami

Modified Install:

To modify composer.json before installation:

  1. Run:

    composer create-project --no-install drupal/recommended-project my_site_name
  2. Edit composer.json to suit your needs.

  3. Run:

    composer install

Managing Modules and Themes

Downloading Modules and Themes:

To download contributed modules or themes:

module_name

Example:

composer require drupal/token

Specifying a Version:

composer require 'drupal/module_name:^1.5'

Defining Custom Directories

Modify the installer-paths section of composer.json:

"extra": {    "installer-paths": {        "modules/contrib/{$name}": ["type:drupal-module"],        "themes/contrib/{$name}": ["type:drupal-theme"]    } }

Downloading Third-Party Libraries

Add Asset Packagist for managing third-party JavaScript and CSS packages:

  1. Add the Composer Installers Extender:

    composer require oomphinc/composer-installers-extender
  2. Add Asset Packagist to the "repositories" section of composer.json:

    "repositories": [    {        "type": "composer",        "url": "https://asset-packagist.org"    } ]
  3. Update the installer-paths section:

    "extra": {    "installer-paths": {        "web/libraries/{$name}": [            "type:drupal-library",            "type:npm-asset",            "type:bower-asset"        ]    } }

Managing Existing Sites

For sites initially installed without Composer, use the Composerize Drupal plugin to generate an updated composer.json.

Updating and Patching

Updating Drupal Core and Modules:

Use Composer to manage updates for both core and contributed modules.

Applying Patches:

Add the cweagans/composer-patches package to handle patches:

composer require cweagans/composer-patches:~1.0 --update-with-dependencies

Update composer.json to include patches:

"extra": {    "patches": {        "drupal/core": {            "3212792: Views button hover effect is odd": "patches/3212792-2.patch"        }    } }

Requiring Specific Commits:

To require a specific commit of a module:

composer require drupal/eck:1.x-dev#ecf376

By following these guidelines, you can effectively manage your Drupal projects and dependencies using Composer.