8.2.x module.inc module_load_include($type, $module, $name = NULL)
8.0.x module.inc module_load_include($type, $module, $name = NULL)
8.1.x module.inc module_load_include($type, $module, $name = NULL)
6.x module.inc module_load_include($type, $module, $name = NULL)
7.x module.inc module_load_include($type, $module, $name = NULL)

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.

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.

46 calls to module_load_include()
dashboard_admin_blocks in modules/dashboard/dashboard.module
Page callback: Builds the page for administering dashboard blocks.
form_get_cache in includes/form.inc
Fetches a form from cache.
form_load_include in includes/form.inc
Ensures an include file is loaded whenever the form is processed.
forum_overview in modules/forum/forum.admin.inc
Form constructor for the forum overview form.
hook_user_cancel in modules/user/user.api.php
Act on user account cancellations.

... See full list

File

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

Code

function module_load_include($type, $module, $name = NULL) {
  static $files = array();

  if (!isset($name)) {
    $name = $module;
  }

  $key = $type . ':' . $module . ':' . $name;
  if (isset($files[$key])) {
    return $files[$key];
  }

  if (function_exists('drupal_get_path')) {
    $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
    if (is_file($file)) {
      require_once $file;
      $files[$key] = $file;
      return $file;
    }
    else {
      $files[$key] = FALSE;
    }
  }
  return FALSE;
}

Comments

Wim Leers’s picture

heylookalive’s picture

For brevity, like this if trying to load "includes/flag.pages.inc" from the flag module.
module_load_include('inc', 'flag', 'includes/flag.pages');

sagar.choudhary’s picture

Thanks dude for concise example, this help me a lot

mohmmadalabed’s picture

Upvote +100 it works and helps me

DanChadwick’s picture

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.

I don't see how this can apply to module or theme developers because they do not know the path to the file (e.g. sites/SITENAME/modules/...). The choices would appear to be either a relative include path, a path derived from dirname(__FILE__), or drupal_get_path, or something that calls drupal_get_path (such as this function).

mikeejt’s picture

For the case of loading files w/in a module (i.e. other inc files), why would we use this over the files[] defined in the .info file? Isn't the dynamic class/file loader specified by files[] a better alternative?

kitt2012’s picture

My current understanding is that you would use the files[] syntax in your info file if you were declaring class or interface functionality in your .inc file.
If you require to call functions, then the module_load_include is required.

as per http://drupal.org/node/1762288#comment-6749026

TechNikh’s picture

use drupal_load() to load a .module file
http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupa...

drupal_load('module', 'browscap');

rakesh.gectcr’s picture

create node programatically, adding a youtube embed to a field

<?php
$node = new StdClass();
$node->type = 'card';
$node->language = LANGUAGE_NONE;
$node->title = "Creating file node youtube";
$node->field_card_type['und'][0]['value'] = 'video';
$video_path = "http://www.youtube.com/watch?v=FmsgX1LkeRE";
module_load_include('inc', 'media_youtube', 'includes/MediaInternetYouTubeHandler.inc');
$obj = new MediaInternetYouTubeHandler($video_path);
$file = $obj->getFileObject();
$file->display = 1;
file_save($file);
$node->field_card_upload_video['und'][0] = (array) $file;
node_save($node);
?>
rhett.prichard’s picture

This is why I'm using module_load_include.
If I disable my module, my constants are not loaded when my hook_uninstall runs, so I am loading the *.module file in the uninstall function to get them back.
Does anyone else think I'm missing something, it just doesn't feel right.

lhridley’s picture

Note: Your base file name parameter needs to include the path relative to the module root to successfully load files located in module subdirectories, even though those files have been declared in the .info file.

Chris Gillis’s picture

Running module_load_include() in the global scope (i.e. in your .module file without being in a function) will break if your module is also implementing hook_boot(). The reason is because the module will have in {system} table 'bootstrap' = 1, which makes it load before drupal_get_path() is defined. If module_load_include() doesn't find drupal_get_path() it will fail silently, and you will get errors like PHP Fatal error: Call to undefined function.

To fix this, run your module_load_include() function inside hook_init().

empesan’s picture

Comments above there are examples with file's name including .inc extension. The right way to load a .inc file from other .inc file shouldn't include extension in the function's third parameter (name of the file).

kkalaskar’s picture

When ever I add module_load_include or require_once into my custom module, code take too much time to load some times it throws 504 gateway time error.