In Drupal 7 I can have a module inside another module. Is this still the case in Drupal 8 and how would I namespace classes inside the sub module?

Another thing I can do in Drupal 7 is to create sub folders inside the modules folder to group my modules.


--- / all
--- / --- / modules
--- / --- / --- / Collection_Alpha
--- / --- / --- / --- / module_a
--- / --- / --- / --- / module_b
--- / --- / --- / Collection_Beta
--- / --- / --- / --- / module_x
--- / --- / --- / --- / module_y

Is this still possible in Drupal 8? If yes, how would I have to namespace my classes so the autoloader can resolve it to the correct folder structure?

e.g.: Drupal\Collection_Alpha\module_a\myClass1 ?

  • 2
    I'm pretty sure namespace resolution will be registered as "/path/to/module/src", so you shouldn't need to do anything different for the 'sub' modules. i.e. main module will be Drupal\my_module in 'my_module/src', sub module will be Drupal\my_submodule in 'my_module/modules/my_submodule/src', and so on – Clive Jan 5 '15 at 12:09
  • Thanks Clive, that makes sense! What about sub folders? Any idea if this is still supported and how that would look like with autoloading a class? – dustinmoris Jan 5 '15 at 12:43

@Clive is correct.

The namespace is always Drupal\{module_name}. Where that module lives is irrelevant (as long as it is somewhere where Drupal can find it). That namespace always points to $path_to_module/src.

It would not possibly work in any other way. Different sites have modules in a different structure and different places, but the same code has to work no matter where.

| improve this answer | |
  • Thanks Clive and Berdir! I guess I have one more follow up question: Is it possible to register an additional namespace which points to the modules folder as well? e.g.: DrupalModules\{module_name} to point to $path_to_module/src? I am asking to better understand how to design our classes today to have little friction when moving to D8 one day. – dustinmoris Jan 5 '15 at 14:08
  • 1
    If the classes are Drupal-specific (i.e. you're not writing a standalone library), it would arguably be better to keep them under \Drupal for consistency's sake. But interesting question nonetheless, I'd encourage you to ask it as a separate question. In fact if you don't, I probably will :) – Clive Jan 5 '15 at 17:46
  • 1
    You probably could, yes, see system_register() and drupal_classloader_register(). Those helper functions hardcode "Drupal\", but you could get the class_loader service directly. But as @Clive said, I would not recommend doing it, there isn't really a reason to do so, unless you're including an external library. – Berdir Jan 5 '15 at 20:41
  • Coolio, that makes sense! Thanks guys! @Clive: As you suggested I created drupal.stackexchange.com/questions/142309/… – dustinmoris Jan 5 '15 at 22:03
  • You can check the mongodb module already to see the structure. – user49 Jan 7 '15 at 7:08

The namespaces is based on PSR-4 autoload, check the specification http://www.php-fig.org/psr/psr-4.

If you want to sub-modules, You can use the next folder structure. d8:


The namespace for the "my_module":


The namespace for the "my_sub_module":


See PSR-4 namespaces and autoloading in Drupal 8 for more information.

| improve this answer | |
  • Thanks dmouse. Initially I posted my question on the linked resource on Drupal.org and now I have edited my own question to point to this thread as well ;) – dustinmoris Jan 5 '15 at 22:09

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.