1 of 32

Managing Your Drupal Project with Composer

matt glaman | @nmdmatt | mglaman

2 of 32

Matt Glaman

Senior Drupal Consultant @ Commerce Guys

Co-maintainer of Drupal Commerce

Author of Drupal 8 Development Cookbook� http://bit.ly/d8-dev-cookbook

3 of 32

Dependency

Management

It ain't’ new.

PIP

4 of 32

5 of 32

composer

The de facto dependency management tool for PHP

6 of 32

composer.json

defines metadata about the project and dependencies for the project

7 of 32

composer.lock

computed information about dependencies and expected install state

8 of 32

composer install

downloads and installs dependencies�will install off of lock file�if no lock file, acts as update

9 of 32

composer update

updates defined dependencies�rebuilds the lock file�generates autoloader

10 of 32

composer require

adds a new dependency, updates the JSON and .lock file.�updates autoloader

11 of 32

composer remove

removes a dependency, updates the JSON and .lock file�updates autoloader

12 of 32

13 of 32

New to Drupal

Drupal never had dependency management. �Drush kind of helped.�Still a little rocky

14 of 32

Installing Drupal

15 of 32

Install from packaged archive

Drupal.org has a packaging system which provides zip and tar archives.

These archives come with all third party dependencies downloaded.

16 of 32

Install via Composer template

Creates your project in some-dir�Contains vendor and web directory�Follows current standard application structure

https://github.com/drupal-composer/drupal-project

composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interaction

17 of 32

Adding dependencies to Drupal

18 of 32

Without Composer (or any tools.)

19 of 32

With Composer

composer require drupal/panels

�"require": {

"drupal/panels": "3.0-beta4",

}

Use Composer require command to add the dependency

Your composer.json should have the following

BUT WAIT. There’s currently a catch….

20 of 32

Drupal.org Composer Façade

https://packages.drupal.org/8

Repository endpoints provide packages

Drupal.org provides a repository endpoint (beta)

Allows Drupal.org projects to be installed via composer

Allows Drupal.org projects to specify 3rd party libraries

Initial community initative: https://github.com/drupal-composer

21 of 32

Adding the endpoint to composer.json

composer config repositories.drupal composer https://packages.drupal.org/8

Your composer.json will now have

"repositories": {

"drupal": {

"type": "composer",

"url": "https://packages.drupal.org/8"

}

},

22 of 32

Version constraints

~8.2.1: >=8.2.1 <8.3.0��~8.2: >=8.2 <9.0.0��^8.2.1: >=8.2.1 <9.0.0

8.2.0: specifically 8.2.0

Core: 8.2.0, 8.2.1, 8.2.2, 8.3.0-rc1, etc.��Contrib: 8.x-2.0 == 8.2.0, 8.x-2.10 == 8.2.10

23 of 32

Updating dependencies

24 of 32

Without Composer (or any tools.)

25 of 32

With Composer

composer update drupal/panels --with-dependencies

Use Composer update command

--with-dependencies

allows all of the package’s dependencies to be updated

Alternatively, could just run composer update for all the things

26 of 32

Updating Drupal Core

27 of 32

Without Composer (manually)

  1. Download latest 8.x.x archive
  2. Remove your core and vendor directories
  3. Make note of changes to .htaccess, composer.json, or robots.txt.
  4. Apply archive contents to site
  5. Re-apply changes to .htaccess, composer.json, or robots.txt.
  6. Run update.php

https://www.drupal.org/docs/8/update/update-procedure-in-drupal-8

28 of 32

With Composer

Modify the shipped composer.json and move drupal/core to a requirement.

"require": {

"composer/installers": "^1.0.21",

"wikimedia/composer-merge-plugin": "~1.3",

"drupal/core": "~8.2"

},

"replace": { },

run composer update drupal/core --with-dependencies and have an up to date Drupal.

29 of 32

With Composer project template

composer update drupal/core --with-dependencies

30 of 32

PATCHES!

31 of 32

Using patch files with Composer

Require cweagans/composer-patches as a dependency�It is a Composer pluginSpecify patches in the extra definition�Applies patches on update and install

"extra": {� "patches": {� "drupal/commerce”: {� "#2805625 Drupal.org": "https://www.drupal.org/files/issues/add_a_new_service_to-2805625-4.patch",� "#2805625: GitHub": "https://github.com/drupalcommerce/commerce/pull/511.patch"� }� }� }

32 of 32

Resources