Drupal project codebase can and should be managed with Composer, as it provides better and more reliable results when building codebase. Drupal Console is replacing Drush -based workflows fast.
Training 20.12.2016 by Perttu Ehn
5. Drush recap
“Drush is a command line shell and Unix scripting interface for Drupal.“
- www.drush.org
Build codebase based on the recipe (.make-file)
$ drush make my-makefile.make
Download and enable modules, themes
Set variables, reset passwords, rebuild or revert features…
Write your own custom commands to do practically anything with Drupal
Drush is not a dependency manager, but a fixed recipe for the codebase
10. Set up a project repository (localdev…!)
You add project requirements
$ composer require drupal/drupal
Composer will solve requirement's dependencies
Commit composer.json and composer.lock -files to Git
Don’t commit code hosted elsewhere - Drupal core,
modules, libraries
Workflow with the Composer
11. Use Composer template for Drupal, it comes with
steroids!
$ composer create-project drupal-composer/
drupal-project:8.x-dev coolio --stability
dev —no-interaction --prefer-dist
Workflow
Image: http://www.e-steroid.com/steroid-articles/negative-effects-of-steroids.html
12. Use Composer template for Drupal
$ composer create-project drupal-composer/
drupal-project:8.x-dev coolio --stability
dev --no-interaction --prefer-dist
Composer may be installed globally or per project1)
Composer == composer.phar -file,“Php ARchive”
Workflow
1) https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
13. Use Composer template for Drupal
$ composer create-project drupal-composer/
drupal-project:8.x-dev coolio --stability
dev --no-interaction --prefer-dist
create-project creates a new project from an existing
project (think git clone)
Workflow
14. Use Composer template for Drupal
$ composer create-project drupal-composer/
drupal-project:8.x-dev coolio --stability
dev --no-interaction --prefer-dist
Use Composer template for Drupal as the base
8.x project version (relates to the Drupal core version)
-dev use dev -version, ie. a git commit (latest one in this
case)
Workflow
15. Use Composer template for Drupal
$ composer create-project drupal-composer/
drupal-project:8.x-dev coolio --stability
dev --no-interaction --prefer-dist
coolio is the directory where the project will be
created
also where composer.json and composer.lock files
will be created
Workflow
22. Web root is in
coolio/web
All libraries are protected from direct access
coolio/vendor/*
vendor code rarely needs to be accessible directly from the
world
index.php
core/update.php (well...)
Folder structure
26. $ composer require drupal/core:8.2.2
Changes core version 8.2.3 -> 8.2.2 (with template project)
$ composer require drupal/ctools
$ composer require drupal/commerce:2.0-beta3
List the modules you have with Drupal:
$ composer show drupal/*
Remove a module - affects composer.json and the codebase, too
$ composer remove drupal/ctools
Add Drush and Registry Rebuild module (Drupal 7 only)
$ composer add drush/drush:7.* drupal/registry_rebuild
Recipes
Get some modules (composer)
zsh requires quoted parameter when it contains an asterisk: $ composer show "drupal/*"
27. You request a (Drupal) package
$ composer require drupal/ctools
Composer finds it (https://packages.drupal.org/8)
Composer updates composer.json -file
Composer downloads the package
Composer checks if this new package has other requirements
(composer.json file)
Composer searches for the requirement… => this is a dependency tree!
Composer dependency resolving 1/2
zsh requires quoted parameter when it contains an asterisk: $ composer show "drupal/*"
28. All good!
&&
Write composer.lock file
OR
Exception!!! Exception!!!
&&
Revert the changes in composer.json file
Composer dependency resolving 2/2
zsh requires quoted parameter when it contains an asterisk: $ composer show "drupal/*"
29. 1. Clone project repository.
2. Run composer install
Composer reads composer.lock -file
Composer retrieves all the required dependencies
Composer generates autoloader
3. That’s it!
(+ some vagrant up and Drupal site installs)
zsh requires quoted parameter when it contains an asterisk: $ composer show "drupal/*"
Fellow developer’s local
30. Run composer update to get the latest available
(version constraints!)
Update code
Write updates to composer.lock -file
Change version (ie change version constraints)
$ composer require drupal/commerce:2.*
zsh requires quoted parameter when it contains an asterisk: $ composer show "drupal/*"
How to do code updates?
32. Three digits; 1.2.3
1st changes might be backwards incompatible (major)
API removals, API changes
2nd changes with API additions, new features (minor)
3rd changes with bug fixes (patch)
Semantic versioning
33. Range
Comparison operators, specified range of allowed versions
>=1.0
>=1.0 <2.0
>=1.0 <1.1 || >=1.2
1.0 - 2.0
Wildcard
* is a wildcard: 1.0.* is the
equivalent of >=1.0 <1.1
https://getcomposer.org/doc/articles/versions.md
Constraints
34. Tilde
~ specifies a minimum version, but allows the last digit
specified to go up: ~1.2 is the equivalent of >= 1.2 <
2.*
Caret
^ sticks closer to semantic versioning; sub-major version
updates should not break anything: ^1.2.3 is equivalent
to >=1.2.3 <2.0.0
https://getcomposer.org/doc/articles/versions.md
Constraints
35. Specify allowed versions, not a specific version
composer.lock -file will contain the specific version which composer
install will use
drupal/core:8.2.2 => drupal/core:~8.2 or drupal/
core:^8.2
drupal/commerce:2.0-beta3 => drupal/commerce:~2
drupal/ctools:~3
Git HEAD (not recommended):
drupal/console:dev-1.x
drupal/console:dev-master - for GitHub
https://getcomposer.org/doc/articles/versions.md
Constraints
37. Problem:
Feature or fix you must have may be in Git, but not in the latest
release
Solution:
Use a specified Git commit
More recipes
38. Use a specified Git commit
; Optionally provide git branch so Drush
; can write that to .info file
projects[ctools][download][branch] = "8.x-3.x"
; Git commit hash
projects[ctools][download][revision] = 1fe3649
More recipes
39. Use a specified Git commit
projects:
ctools:
download:
# Optionally provide git branch so
# Drush can write that to .info file
branch: 8.x-3.x
# Git commit hash
revision: 1fe3649
More recipes
40. Use a specified Git commit
$ composer require drupal/ctools:dev-3.x#1fe3649
More recipes
41. Use a specified Git commit
READ: never use HEAD (“latest whatever code”)
More recipes
42. Problem:
-> Found and fixed a bug in a contrib module, and now you need to
use your fixes (a patch file) from drupal.org? No prob.
-> Perhaps you were lazy and never uploaded the patch? No prob
either.
Solution: Drush and Composer can apply remote and local patches
Patching modules
45. Install module field_group
$ composer require drupal/field_group:1.0-rc4
Edit composer.json and edit (or add) "extra":
"extra": {
"patches": {
"drupal/field_group": {
"A patch with URL”: “https://www.drupal.org/files/issues/
field_group-empty_group_nonnumeric_index-2761159-2-D8.patch",
“Local patch (path relative to composer.json)”: "patches/
field_group-fix_it.patch"
}
}
}
Apply the patches and update composer.lock -file
$ composer install # applies the patch
$ composer update --lock # writes patch info to .lock file
Patching modules
46. There is a module for that… but it requires an external library!
As an example:
Chosen -module is a wrapper for chosen-library
Chosen library must be downloaded separately
Other libraries
Image: https://opensource.com/life/16/1/indiahacks-2016
49. Manually add new item to your repository -section in composer.json:
"repositories": [{
"type": “package",
"package": {
"name": "customlibs/chosen",
"version": "master",
"dist": {
"type": "zip",
"url": "https://github.com/harvesthq/chosen/releases/download/1.4.2/
chosen_v1.4.2.zip",
"reference": “1.4.2"
},
"autoload": {
"classmap": ["."]
}
}],
Request the library
$ composer require customlibs/chosen:1.4.2
Then update your packages as per what you've just done:
$ composer update
NOTE: Chosen modules declares this dependency in Drupal 8 version via composer.json
Other libraries
Image: https://opensource.com/life/16/1/indiahacks-2016
51. Briefly about Drupal Console
Composer manages (Drupal) codebase
Drupal Console does tricks with Drupal itself (Drupal 8)
Help your self by aliasing drupal if it is not in your $PATH
#localdev VM bash alias is
alias drupal="/vagrant/drupal_root/vendor/
drupal/console/bin/drupal"
57. Some random commands
Go nuts!
$ drupal [-l=local-1.example.com] list
$ drupal [--uri=local-1.example.com] list
Get help!
$ drupal help [topic]
Documentation https://drupalconsole.com/docs , also in
Vietnamese and Hindi among some other languages
NOTE: Drupal Console v1.0.0-rc12 (not yet released) is needed
for properly working (automated) multisite installation process
60. Take also look at these
Composer template for Drupal projects
https://github.com/drupal-composer/drupal-project
Improving your Drupal 8 development workflow
by Jesus Manuel Olivas
http://weknowinc.com/talks/2016/drupalgov-workflow
Drupal Console Docs
https://drupalconsole.com/docs