In Drupal 7, you can use module_exists() to check whether a module is enabled (as well as whether it exists).

For example,

if (module_exists('devel')) {

How can you check whether a module is enabled in Drupal 8?

up vote 24 down vote accepted

Just like Clive mention....

service would be preferable to using \Drupal (assuming it's an option in current context)

Here is an example of checking if a module is enable but using a service.

$moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler->moduleExists('devel')){
     // Code Here
  }
  • \Drupal::moduleHandler ends up just making an equivalent call to get the "module_handler" service anyway (see the moduleHandler doco). The key word in Clives comment was injecting, i.e. passing the service to a constructor. – George Apr 12 at 1:10

Like this:

  if (\Drupal::moduleHandler()->moduleExists('devel')) {
  • 2
    It's worth nothing that injecting the module_handler service would be preferable to using \Drupal (assuming it's an option in current context) – Clive Jan 28 '16 at 9:15

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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