Installing Drupal 8 module with dependency

Posted by denutkarsh on Tue, 01/17/2017 - 16:08

Creating a large fully functional module can't be always made with just using internal libraries and scripts. Sometimes the module has to use various external libraries to use different features and hence increase their functionality. Manually managing all the dependencies  is  a painful task and this is where the composer is helpful. Composer makes it easy to manage dependencies.

What is Composer?

Composer is a PHP package management tool to help manage your project or libraries dependencies. It is used by various modules and projects to manage their dependencies. Detailed information about it could be found on this page https://www.drupal.org/project/composer.

Installing Composer

Downloading and using, composer is quite easy. According to the official composer website, just enter the below commands in terminal.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '61069fe8c6436a4468d0371454cf38a812e451a14ab1691543f25a9627b97ff96d8753d92a00654c21e2212a5ae1ff36') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

install

Now we can actually use this composer.phar file to install the modules.

Note:* You  can also add this composer.phar to environment, so that it could be easily accessed from terminal.

Installing a Drupal 8 Module that needs external libraries

Now, after knowing what composer really is and what we can do with it, its now time to install our first module that needs external libraries. Currently i am choosing this module:- https://www.drupal.org/project/address . This module already contains the instruction to install it. It's actually quite easy to install this module, just follow the below steps:-

1)  Configure the composer to find the location of Drupal 8 modules

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

2) Download the "Address" module and all its dependencies

composer require "drupal/address ~1.0"

Download the Module

Note*: The above image doesn't show installing all the dependencies because i have already run this command before.

Enable the module

Now everything is setup so lets check, if our installed module is working fine or not. First of all, just go to admin/modules and enable the "Address" Module.

Enable Address Module

Test if the module works

Lets see if the installed module is working fine. Lets add a "Address" field to Article.

Go to admin/structure/types/manage/article/fields and then click on "Add new field"

12

Now let's add an article to see the module working

34

Finally i have installed the "Address" module which requires external libraries using composer.