1

I can't find anywhere documentation on how to create a sub-module inside a module in Drupal 8. I have created a custom module for translations I am trying to enable as sub-module.

In the main module, can I simply create a sub_modules folder and place my sub-module, including the submodule.info.yml file and the translations folder? I'm guessing I have to add something into my mainmodule.info.yml file but I have not found anything to help me.

3
  • 1
    You need to define what you mean by a sub-module in order to be able to answer this, as Drupal doesn't have a defined concept of a sub-module per se.
    – Jaypan
    Commented Apr 9, 2020 at 19:08
  • 2
    Convention is to put submodules into a modules folder in your main module and have their machine names prefixed with the main module name. See git.drupalcode.org/project/webform for example. That's all.
    – leymannx
    Commented Apr 9, 2020 at 19:20
  • Cool that is how I did it. However, with the submodule.info.yml file I have to add this line: interface translation server pattern: modules/custom/%project/modules/%project/translations/%project.%language.po. Would that be the correct format? Seems that how the file structure is in the examle. Commented Apr 9, 2020 at 19:59

1 Answer 1

5

Drupal doesn't have the concept of sub-modules. It just happens that it looks for modules in the directories where it expects to find modules, recursively. For example, if you install the CAPTCHA module, Drupal will find also the Image CAPTCHA module, and allow you to install it.

Drupal will not install it automatically, nor will it handle it differently from any other module. This means, for example, that the module still needs to declare its dependency from the CAPTCHA module, if it requires it, which is what the Image CAPTCHA module does in its image_captcha.info.yml file.

name: Image CAPTCHA
type: module
description: Provides an image based CAPTCHA.
package: Spam control
core: 8.x
dependencies:
  - captcha
configure: admin/config/people/captcha/image_captcha

Usually, developers put any "sub-module" in the modules directory, but this is not required from Drupal, nor is it followed by every developer.

5
  • I've tried both - one issue I ran into is that when multiple modules are included as a package, if the 'sub-modules' have a composer.json that manages external dependencies, this can run into problems. I had to split out the modules after the fact when this happened.
    – Jaypan
    Commented Apr 9, 2020 at 19:49
  • Do you mean dependencies that aren't other modules? I am glad you are telling me this, so I will avoid putting modules into other modules. This should be a bug, though, and I hope it will be fixed.
    – apaderno
    Commented Apr 9, 2020 at 20:05
  • Yes, external library dependencies, not Drupal module dependencies. Unfortunately I can't find the thread where I reported it to the infrastructure team to give you a reference.
    – Jaypan
    Commented Apr 9, 2020 at 20:09
  • I found Composer conflict between former submodule and now module, which so far seems the only issue you opened about Composer and sub-modules.
    – apaderno
    Commented Apr 9, 2020 at 20:47
  • 3
    Ahh, nice find, that was it! I couldn't remember exactly what it was. My above comment was a little off - it only ends up being a problem if/when you move the sub module to its own module, and only if/when those modules are hosted on Drupal.org. Anyone reading this, please (mostly) disregard my earlier comment about sub modules.
    – Jaypan
    Commented Apr 9, 2020 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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