Mastering Drupal: Managing custom module dependencies with Composer

How to manage drupal custom dependencies

In this article, we will explore the process of managing dependencies in Drupal custom modules using Composer. Drupal is a powerful content management system that allows developers to create feature-rich websites and applications. Composer, a dependency management tool for PHP, simplifies the process of including external libraries and packages in Drupal projects. We will specifically focus on including the lullabot/amp package as an example. This comprehensive guide will provide you with the information you need to effectively manage dependencies in your Drupal custom modules.

What are Dependencies in Drupal?

In this section, we will explain what dependencies are in the context of Drupal and how they contribute to the functionality of custom modules. Dependencies are external libraries, modules, or packages that your custom module relies on to provide specific features or functionality. Understanding dependencies is crucial for developing modular and maintainable Drupal applications.

Introduction to Composer

Here, we will provide an overview of Composer and its significance in Drupal development. Composer is a popular dependency management tool for PHP that simplifies the process of including external libraries and packages in your Drupal projects. Since Drupal 8. x as a Drupal developer, we pretty much prefer to use composer-managed applications, why because this way we can handle our module dependencies very easy manner. Will discuss its key features and benefits.

There are three ways to handle our custom module dependency,

  1. We can define dependencies in the root-level composer .json
  2. Then define the dependency in our custom module composer.json then add this reference to root-level composer.json as a repository.
  3. Else we can merge the custom module composer.json with the root level composer.json file

Approach 1: Defining Dependencies in composer.json

In this section, we will demonstrate how to define dependencies in the root-level composer.json file. We will use the lullabot/amp package as an example and show you how to specify the required version.

cd DRUPAL_ROOT
composer require lullabot/amp

Setting up a Drupal Custom Module with composer.json

This section will guide you through the process of setting up a custom module in Drupal. We will cover the necessary steps, including creating the module folder, defining the module file structure, and enabling the module in Drupal. Here, we will explain the importance of the composer.json file and how it helps manage dependencies. We will guide you through creating the composer.json file in your custom module’s directory. So as usual we are going to create a module folder and place the necessary files, and run,  composer init this will create a raw composer.json within your module directory. 

{
    "name": "drupal/ac_utilities",
    "description": "Dependency with Active Campaign library",
    "version": "1.0",
    "autoload": {
        "psr-4": {
            "Drupal\\AcUtilities\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Sankar Prakash"
        }
    ]
}

Then you need to add the required third-party dependency like below.

{
    "name": "drupal/ac_utilities",
    "description": "Dependency with Active Campaign library",
    "version": "1.0",
    "require": {
        "lullabot/amp": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "Drupal\\AcUtilities\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Sankar Prakash"
        }
    ]
}

Approach 2: Adding Repositories

Add local repositories and then require the module. Edit the root level composer.json and search for the repositories key and update like below. Sometimes, packages may not be available on Packagist, the default package repository. Here, we will explain how to add additional repositories to your composer.json file, using Drupal.org as an example.

"repositories": [
    {
        "type": "composer",
        "url": "https://packages.drupal.org/8"
    },
    {
        "type": "path",
        "url": "web/modules/custom/utilities"
    }
],

Approach 3: Running Composer to Manage Dependencies

Merge custom module composer.json with root level composer.json file. Once the composer.json file is set up, we need to run Composer to manage the dependencies. We will explain how to run the composer install command to download the required packages into the vendor directory.

"extra": {
    "merge-plugin":{
        "include": [
            "web/modules/custom/*/composer.json"
        ],
        "recurse": true
    }
},

Then run,

composer require wikimedia/composer-merge-plugin

Composer Merge Plugin is intended to allow easier dependency management for applications that ship a composer. Now we are all set to use the third-party dependency in our custom module functionality. Now we are ready to use and include the lullabot/amp package in your Drupal custom module adding the package as a dependency, running Composer, and ensuring the autoloader includes the necessary classes.

Hope this article will help you understand how to manage dependencies with Composer and will enhance your Drupal development skills. Embrace the power of Composer and take your Drupal custom modules to the next level by leveraging external packages to extend functionality and improve efficiency.

Reference: https://www.drupal.org/docs/develop/using-composer/managing-dependencies-for-a-custom-project

 

Leave a Comment