4

I have a custom module that I have defined in the [root]/modules/[custom module] directory. I want it to get activated as soon as someone installs my theme rather than handing over user the custom modules folder and asking to manually install that module one by one via the Extend option on the dashboard. How can we get that done in Drupal 8?

  • I guess it's rather the other way around. Theme activation on modul install. – leymannx Jun 14 '18 at 8:56
  • 2
    See this issue Allow themes to declare dependencies on modules – 4k4 Jun 14 '18 at 9:01
  • You better place your custom modules inside [root]/modules/custom/[custom module] and all contrib modules inside [root]/modules/contrib/[contrib module]. Same pattern for themes. – leymannx Jun 14 '18 at 10:56
2

As @4k4 mentioned there's ongoing development to Allow themes to declare dependencies on modules which hopefully lands in core with 8.6.0. Until then you are a little bit out of luck, except you find a place somewhere in your theme to trigger the following line of code:

\Drupal::service('module_installer')->install(['MYMODULE']);

Alternatively you could provide a second module which provides some button in the back-end which on-click triggers the above line PLUS the following two lines all at the same time.

\Drupal::service('theme_installer')->install(['MYTHEME']);
\Drupal::service('theme_handler')->setDefault('MYTHEME');

The background behind the issue mentioned above is that themes can't provide Drush 9 commands. As Drush 9 commands depend on certain services. But themes ain't allowed to provide these services. See the following issue: Allow themes to provide services.yml which was closed as won't fix.

The reason to close the service-issue (in a nutshell) is to keep things clean (from an OOP point of view) and don't let themes become other modules. Themes should provide templates, styles, scripts. Whereas modules should provide everything OOP-related, including services.

And as themes now (because of this decision) can't provide Drush commands anymore there must be away to allow them to rely on companion modules to provide useful commands/scripts and libraries for (sub-)theme development and maintenance. Which then will probably get activated automatically upon theme activation. So let's look forward and contribute to get this into core ASAP!

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.