I think I understand the structural differences of a .inc file (compared to a .module), but could someone describe the design differences? I see drupal example modules calling an .inc file with hook_menu, or I see call .inc file for function defintions.

  • Under what circumstances does one put code in a .inc file? Any general design guidlines that some adhere to?
  • Is there any advantage other than clarity as to why one would use an (or multiple) .inc file? performance? versioning?


  • 1
    You can't get a better explanation than the accepted answer to this question in my opinion :)
    – Clive
    Jan 3 '13 at 19:00
  • 3
    personally, if i have a mod that supports multiple urls or whatever, i use a .inc per url. just organization i guess instead of dumping random functions into one large .module file. but as @Clive's post references, its really just personal opinion or what you are used to. no right or wrong here.
    – au_stan
    Jan 3 '13 at 19:21
  • 1
    indeed, I have used modulename.preprocess.inc, modulename.node.inc, modulename.menu.inc etc to break up large modules into reasonable chunks of related functions, but the only real reason from any performance standpoint to use them outside of development organization is probably if your module has some rather large functions that are rarely called that you don't want your php engine to parse each time it's loaded. you could then bring them in when needed, much like how things are done with menu callback includes.
    – Jimajamma
    Jan 3 '13 at 19:36
  • For what it's worth I group my hook implementations and helper functions and categorize their doc blocks using an appropriate naming convention (in CAPITALS). So as I scroll through my module's code I can see separation between the different groups of functionality. I looked in the commenting standards to see if a system already existed, but I could see one.
    – dbj44
    Feb 11 '14 at 16:02

As general rule, I would put in the module file the code that is required more often (e.g. helper functions used from more than one function), and in .inc files the code that is not used so often, or that is used for specific pages.

Since Drupal 6, the code automatically loads the files containing the page callbacks, or the form builders used for the menu items. For that reason, page callbacks for administrative pages are normally put on .admin.inc files, while page callback for normal pages are put on .pages.inc files.

Since Drupal 7, files containing classes are automatically loaded when a class is instantiated. Drupal 7 allows modules to define in which files their hooks are defined (through hook_hook_info()). For example, system_hook_info() defines the .tokens.inc files as files where hook_token_info(), hook_token_info_alter(), hook_tokens(), and hook_tokens_alter() implementations can be found; in this way, those files are automatically loaded when one of those hooks are required.
This allows to further split the code in files that are loaded when necessary, and code that is always loaded from Drupal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.