3

What way should I use to load a file from a module?

I am writing a module which is divided into 3 parts

  • Custom core will be in the module which contains basic functions required for project
  • Custom notification section will handle the notifications to show on site (It has a section on site where user can see all notifications.)

  • Custom mail will handle mail (Users will get email on each activity.)

So I have now a module file and 2 include files, but I am confused on how to include them in .module file.

There are 3 different methods available, but I am not sure which one is best and what the major differences between them are.

  1. require_once drupal_get_path('module', 'custom_module')
  2. files[] = mail.inc
  3. module_load_include('inc', 'custom_module', 'mail.inc')

I tried them, and they all are working fine for now; I don't have much experience to guess future issues.

2
  • You can include by using function module_load_include
    – Nishant
    May 19, 2015 at 10:56
  • yes i can use it but why it is best ?
    – Vikram
    May 20, 2015 at 3:39

2 Answers 2

2

module_load_include() is a wrapper around require_once, with some sugar, so those are essentially equivalent. In the Drupal world, it would make sense to use the Drupal-specific function.

The files[] directive is for class autoloading - you don't use it to include files as such, just to make classes in those files available automatically when you need them.

You shouldn't use module_load_include() at the top of a module (or any other) file, it should be used only after Drupal has been bootstrapped. From the docs:

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.

4
  • so i can use require_once at top of the .module file ?
    – Vikram
    May 19, 2015 at 12:16
  • Absolutely, that's the recommendation from the core developers
    – Clive
    May 19, 2015 at 12:24
  • Can we safely use drupal_get_path() (common.inc) in combination with require_once, for getting the path to the module folder? Or is that also only loaded in a full bootstrap? May 20, 2015 at 11:21
  • @Clive could you help me out here? May 22, 2015 at 8:17
-1

The best way for drupal to know you have included files is having it in the files[] directive, as Clive says this is perfect for classes, but you can also use it for any files.

Using module_load_include() you can add some loading logic, you can load also files from another module, and for best performance you can load them from within a function or a conditional structure. That way is only loaded when you are using it.

2
  • 1
    No, you cannot use files[] for any files, since Drupal will use it just to load classes.
    – apaderno
    May 21, 2015 at 11:50
  • I'm using it now in a drupal 7 module May 21, 2015 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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