1

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?

1

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
3
  • so where do I put the field group module folder? – Deryan Thimoti May 7 '20 at 7:40
  • As developer of a module, you don't need to put that module in any place. It's who uses your module that needs to download any module from which yours depends. – kiamlaluno May 7 '20 at 7:43
  • thank you for the answer – Deryan Thimoti May 8 '20 at 2:43
3

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.

1
  • thanks for the answer and the detailed explanation – Deryan Thimoti May 8 '20 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.