Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

In Drupal 8, only procedural code for e.g. hooks and theme functions remains in .module and .profile files.

Various modules and installation profiles do not have a use-case for implementing hooks or any procedural code. Therefore,

  1. .module and .profile files are optional in Drupal 8. It is no longer required to create an empty PHP file.
  2. all extensions are solely discovered based on their .info.yml files only.

To make this possible, the values of ModuleHandler::getModuleList() and ModuleHandler::setModuleList() have changed:

Instead of a list of pathnames, each array element is an instance of Drupal\Core\Extension\Extension now.

Drupal 7

foreach (module_list() as $name) {
  $path = drupal_get_path('module', $name);
  // yields e.g. 'modules/node'

  $infofile = $path . '/' . $name . '.info';
  // yields e.g. 'modules/node/node.info'

  $pathname = drupal_get_filename('module', $name);
  // yields e.g. 'modules/node/node.module'
}

Drupal 8

foreach (\Drupal::moduleHandler()->getModuleList() as $name => $module) {
  $path = $module->getPath();
  // yields e.g. 'modules/node'

  $infofile = $module->getPathname();
  // yields e.g. 'modules/node/node.info.yml'

  $pathname = $module->getExtensionPathname();
  // yields e.g. 'modules/node/node.module'
  // NOTE: Returns NULL if there is no .module or .profile file.

  $filename = $module->getExtensionFilename();
  // yields e.g. 'node.module'
  // NOTE: Returns NULL if there is no .module or .profile file.
}
Impacts: 
Module developers