Programmatically install Drupal 8 module

Programmatically install Drupal 8 module timmillwood Tue, 16/05/2017 - 07:41

There are times, often in tests or upgrade paths, where we want to programmatically install a module. Here's how:

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

Comment

This code can go pretty much anywhere. My most recent use for it was in a test where I wanted to add some content, then install a module, and test how the module reacted to the content which was there before the module was installed. For this I just added the code in the test method of a functional browser test.

Add new comment