Adding dependencies in .info.yml (core, contrib, or custom modules)for Drupal 8 , 9 , 10 , and 11

Last updated :  

In our previous lesson, you learned how to create and configure the .info.yml file, setting the stage for your module to be recognized by Drupal. Now, it's time to delve deeper into enhancing your module by adding dependencies. Adding dependencies can unlock and extend the functionality of your module by leveraging existing features provided by core, contributed, or custom modules.

Understanding Dependencies in Drupal

Dependencies allow your module to require and automatically enable other Drupal modules. This ensures the necessary components are present and loaded in the correct sequence, which is especially important for modules that rely on specific functions or features provided by other modules.

For instance, if your custom module adds features to the "block" module, you must ensure the "block" module is enabled for yours to function properly.

Adding Dependencies to the .info.yml File

Dependencies are specified in your module's .info.yml file under a dependencies section. Here's how you can add these within your hello_world.info.yml:

name: 'Hello World'
type: module
description: 'A simple hello world module for learning purposes.' 
package: Custom
core_version_requirement: ^9.3 || ^10.0
version: '1.0.0'

dependencies:
  - drupal:system
  - drupal:block
        
  • drupal:system: A core dependency, which is already available in all Drupal environments, required by almost every module as it is the core system of Drupal.
  • drupal:block: Indicates that your module requires the "block" module to be enabled for certain functionality, making it essential if you're extending block functionalities.

Add each dependency as a YAML list item under the dependencies key. This tells Drupal to check for these modules during installation and enables them if they're not already active.

Types of Dependencies

Dependencies in Drupal can be categorized into three types:

1. Core Dependencies

These are dependencies on modules that are part of the core Drupal project. They are typically prefixed with drupal:. For example, drupal:node or drupal:user are core modules.

2. Contributed Dependencies

Modules available through Drupal.org but not included with the core installation. You indicate these just like core modules, using drupal: and their project name. For example, drupal:views.

3. Custom Dependencies

If you have a collection of internally created modules, you can declare dependencies on those as well using the module’s machine name, such as drupal:custom_module.

It's important to ensure that any contributed or custom dependencies are available in your environment for seamless deployment and functionality.

Implications of Adding Dependencies

Understanding the implications of adding dependencies is crucial:

  • Automatic Activation: Declaring a module as a dependency will cause it to become automatically enabled when your module is enabled, ensuring seamless user experience and reducing manual setup steps.
  • Conflict Resolution: Properly defined dependencies avoid conflicts that can arise from missing module functionality, enhancing site stability.
  • Maintainability: Helps in maintaining clear module relationships, especially in large projects where module functionalities overlap.

Testing Your Dependencies

After adding dependencies, revisit your module on the Drupal admin interface:

  1. Go to the Extend page.
  2. Ensure your module, along with its dependencies, is listed and activated seamlessly.
  3. Inspect logs for any missing or conflicting dependencies that need attention.

This testing is vital for confirming the setup functions as expected and is free from errors.

Conclusion

By mastering how to add dependencies in your .info.yml file, you've advanced your capability to build robust, interconnected Drupal modules that leverage existing functionalities. In our next lesson, we'll explore Grouping modules using the package key in .info.yml, allowing you to better organize your modules within the Drupal admin interface. Stay tuned for more!