Question

I have created a module (travel_list) that uses the Field Group module. I want the Field group module to be automatically downloaded and installed whenever the travel_list module is installed.

How can I achieve this?

Was it helpful?

Solution

With Drupal, a module doesn't download and install the modules from which it depends. Doing that would fail, as the system account used to run PHP should not have write access to the directory containing the modules. Letting PHP write files in the directory containing the modules, especially if those files contain PHP code, is considered a security issue.

Instead, modules declare their dependencies from other modules in their .info.yml file. In your case, the travel_list.info.yml file would contain the following lines.

dependencies:
  - field_group:field_group

It should also declare its dependencies from Drupal core modules. For example, in the case it also depends from the Search module, it should contain the following lines.

dependencies:
  - drupal:search
  - field_group:field_group

OTHER TIPS

If a module depends on another module, you have to declare it as a dependency. The way Drupal handles this, is through the *.info.yml files, which define the basic module attributes.

In 'travel_list.info.yml' add this key:

dependencies:
  - field_group:field_group

Doing that, whenever you install the module 'travel_list' it will enforce you to install 'field_group'; this means that if you try to install 'travel_list' module and 'field_group' is not installed, the installation will abort reporting that 'field_group' module is needed.

Developing an script that downloads and triggers the module installation is far away from good practices and can lead to a number of unpredicted situations, starting by the fact that the required modules of your Drupal are defined in the composer.json.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top