Drupal - How can I check if a module is enabled?

Just like Clive mention....

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

Here is an example of checking if a module is enabled using a service (without injecting it).

$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('devel')) {
  // Code Here
}

Like this:

  if (\Drupal::moduleHandler()->moduleExists('devel')) {

Tags:

8