Skip to main content

Composer and Drupal 8 module development

Published on 2017-04-14
This blog-post I also published here Dx-Experts.nl

Did you know that when developing a module(s) locally you can now easily do this in separate git repositories.
This is very useful because it gives the opportunity to both use a separate Git for your custom module and an IDE like PHPstorm+Xdebug for the Drupal integration at the same time.

The directory structure will look like this:

~/DrupalSites/my_drupal_project  <-- contains the Drupal installation.
~/DrupalModules/my_drupal_module <-- contains the module code.

Instead of copy-pasting the module inside the Drupal installation, we will use a symlink. This is managed by composer.

The symlink looks like this:

$ cd ~/DrupalSites/my_drupal_project/modules/contrib
$ ls -l
my_drupal_module -> ../../../../DrupalModules/my_drupal_module

How it works

1. Install Drupal as you like in ~/DrupalSites/my_drupal_project . I used 8.3+ here.
2. Create the custom module in ~/DrupalModules/my_drupal_module.
3. In the custom module add composer.json:

{
    "name": "drupal/my_drupal_module",
    "description": "My Drupal module.",
    "type": "drupal-module",
    "license": "GPL-2.0+"
}

4. In the Drupal project you add the following items to composer.json via the command-line

$ composer config repositories.my_drupal_module path ~/DrupalModules/my_drupal_module
$ composer require drupal/my_drupal_module:"*"

 

To validate when you check the composer.json of the Drupal project 2 items new:

    "require": {
        "drupal/my_drupal_module": "*"
    },

and

    "repositories": [
        {
            "type": "path",
            "url": "~/WebWork/DrupalModules/dxex_classifier"
        },

Read more:
https://github.com/composer/composer/issues/2171#issuecomment-255604186
https://www.jeffgeerling.com/blog/2017/tips-managing-drupal-8-projects-...
https://www.jeffgeerling.com/blog/2017/composer-and-drupal-are-still-st...

Author: Niels de Feyter