Drupal7: on splitting module code into separate .inc include files

HOT TIP: this content is flagged as highly recommended !

Some key points about use of .inc files with Drupal7

Example of loading include file in same folder as the .module:

./includes/language.inc: *   module_load_include('language', 'inc', 'language.negotiation');
./includes/module.inc: *   module_load_include('inc', 'node', 'node.admin');
./includes/module.inc: *   module_load_include('inc', 'node', 'content_types');

Example of loading include file in a subfolder includes:

./sites/all/modules/entity/entity.module:  module_load_include('inc', 'entity', 'includes/entity.ui');

From Splitting module code into separate include files:

if your function points to a file other then your .module you can use:

function node_menu() {
  $items['admin/content'] = array(
    'title' => 'Content',
    'description' => 'Find and manage content.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('node_admin_content'),
    'access arguments' => array('access content overview'),
    'weight' => -10,
    'file' => 'node.admin.inc',

This is from the node.module, node.admin.inc is in the same directory as the node.module.

from redsd 2012

From Classes and interfaces can be auto loaded (aBrookland 2012):

If you put class or interface declarations into a .inc file (eg my_module.foo.inc) and then reference that file in the files section of my_module.info then Drupal 7 will auto discover and remember the classes and interfaces in that .inc file and use PHP auto loading functionality whenever any of them are used so [there] is no need to include or require the .inc file.

The my_module.info would look like this:

name = My module
core = 7.x
files[] = my_module.foo.inc
Drupal 7 only does the auto discover when the module is enabled so if you're developing a module and add the files[] line after the module is already enabled you'll need to clear the cache to get it working.

If you want to put functions in and inc file then you still need to include the file but the Drupal way of doing it is with module_load_include() which in this instance would be called:

 module_load_include('inc', 'my_module', 'my_module.foo');

This way you don't need to know the full path to your module.

From http://drupal.stackexchange.com/questions/54051/difference-between-inc-a...

... Modules being used in specific cases use conventions similar to .admin.inc. That file typically includes all the code necessary to run the administrative interface, and a file node.pages.inc for example contains the code executed for other menu callbacks.

Dec 24 '12 at 11:35 Alex Gill

  1. .module files are loaded on every load page, whereas .inc files are loaded only when they are requested.
  2. By convention .module files should contains only those functions which are implementation of hooks. Following this standard improves the performance too.
  3. .inc files can be included by mentioning the path in hook_menu(), or by making an explicit using module_load_include('inc', 'node', 'node.admin').
  4. If any of the .inc files contains class objects, it should be mentioned in the .info file of the module.

From http://drupal.stackexchange.com/questions/77545/how-to-move-block-functi...

The files directive is used to include files containing classes, check [.info file] documentation. You could include the file in the .module file.

require_once __DIR__ . '/includes/blocks.inc';

Edit: Included the other solutions added by @Beebee and @Jimajamma in the answer:

require_once  drupal_get_path('module', 'mymodulename') . '/includes/blocks.inc';
module_load_include('inc', 'MY_MODULE', 'FILE_NAME');
// This method is including a file residing in the module folder not in a subdirectory.

Jun 26 '13 at 14:56, Sorin S.

or you can also use 'drupal_get_path('module', 'mymodulename)' as a prefix instead of __DIR__ but I don't see anything wrong with either way. – Beebee Jun 26 '13 at 15:32

From Writing module .info files (Drupal 7.x):

files (Optional)
Drupal now supports a dynamic-loading code registry. To support it, all modules must now declare any code files containing class or interface declarations in the .info file, like so:

name = Really Neat Widget
files[] = tests/example.test

When a module is enabled, Drupal will rescan all declared files and index all the classes and interfaces that it finds. Classes will be loaded automatically by PHP when they are first accessed.

From http://stackoverflow.com/questions/3520928/how-to-split-the-code-of-a-mo...

A module should never unconditionally load a file it needs calling module_load_include() from outside a function.

Aug 19 '10 at 12:35 kiamlaluno

Typical files used ...

  • .admin.inc for all administration stuff, menu callbacks, forms etc.
  • .pages.inc for frontend menu callbacks.
  • .theme.inc for theme functions, preprocess hooks etc.
  • .forms.inc for non admin forms and their handlers.

Aug 19 '10 at 12:34 googletorp

On hook_hook_info()

From http://drupal.stackexchange.com/questions/77545/how-to-move-block-functi...

Normally hooks do not need to be explicitly defined. However, by declaring a hook explicitly, a module may define a "group" for it. Modules that implement a hook may then place their implementation in either $module.module or in $module.$group.inc. If the hook is located in $module.$group.inc, then that file will be automatically loaded when needed. In general, hooks that are rarely invoked and/or are very large should be placed in a separate include file, while hooks that are very short or very frequently called should be left in the main module file so that they are always available.


* Implements hook_hook_info().
function mymodule_hook_info() {
  $hooks['block_info'] = array(
    'group' => 'block',
  $hooks['block_configure'] = array(
    'group' => 'block',
  $hooks['block_view'] = array(
    'group' => 'block',
  return $hooks;

Then you should place those hook functions into a file called mymodule.block.inc and the same file should be available in the module directory.

Jun 27 '13 at 13:18 Mathankumar

See also Organize your module code with hook_hook_info() (Drupal 7):

Drupal 7 introduced a wonderful new hook called hook_hook_info(). It's an optional hook that module developers can use to organize their code and increase performance (a bit). It basically allows you to declare the hooks that your module implements, and optionally separate them into multiple files, so they aren't all in one *.module file. ..

Please do read about the caveats described there, this new hook does not yet "play nice" !