You are here

function module_load_include in Drupal 9

Same name and namespace in other branches
  1. 8 core/includes/module.inc \module_load_include()
  2. 6 includes/module.inc \module_load_include()
  3. 7 includes/module.inc \module_load_include()
  4. 10 core/includes/module.inc \module_load_include()

Loads a module include file.

Examples:

// Load node.admin.inc from the node module.
module_load_include('inc', 'node', 'node.admin');

// Load content_types.inc from the node module.
module_load_include('inc', 'node', 'content_types');

Do not use this function to load an install file, use module_load_install() instead. Do not use this function in a global context since it requires Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file' instead.

@todo The module_handler service has a loadInclude() method which performs this same task but only for enabled modules. Figure out a way to move this functionality entirely into the module_handler while keeping the ability to load the files of disabled modules.

Parameters

$type: The include file's type (file extension).

$module: The module to which the include file belongs.

$name: (optional) The base file name (without the $type extension). If omitted, $module is used; i.e., resulting in "$module.$type" by default.

Return value

The name of the included file, if successful; FALSE otherwise.

31 calls to module_load_include()
content_translation_form_field_config_edit_form_alter in core/modules/content_translation/content_translation.module
Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.
content_translation_form_language_content_settings_form_alter in core/modules/content_translation/content_translation.module
Implements hook_form_FORM_ID_alter() for language_content_settings_form().
content_translation_preprocess_language_content_settings_table in core/modules/content_translation/content_translation.module
Implements hook_preprocess_HOOK() for language-content-settings-table.html.twig.
entity_test.install in core/modules/system/tests/modules/entity_test/entity_test.install
Install, update and uninstall functions for the entity_test module.
hook_user_cancel in core/modules/user/user.api.php
Act on user account cancellations.

... See full list

File

core/includes/module.inc, line 58
API for loading and interacting with Drupal modules.

Code

function module_load_include($type, $module, $name = NULL) {
  if (!isset($name)) {
    $name = $module;
  }
  if (\Drupal::hasService('extension.list.module')) {

    /** @var \Drupal\Core\Extension\ModuleExtensionList $module_list */
    $module_list = \Drupal::service('extension.list.module');
    $file = DRUPAL_ROOT . '/' . $module_list
      ->getPath($module) . "/{$name}.{$type}";
    if (is_file($file)) {
      require_once $file;
      return $file;
    }
  }
  return FALSE;
}