0

I want include PHP file in drupal 8 module. What is the best way for it ?

3

Outside of autoloading, the only option Drupal provides itself is module_load_include(), which literally

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');

The final part of the description is worth taking in separately as it's important:

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.

It's also worth noting this function won't be around forever, the plan is to move it completely into the module handler service.

1
  • Could be that the file doesn't exist. Check the return value, the code of module_load_include is very small so you'll easily be able to work out which part is going wrong. I can't do that from here
    – Clive
    Apr 7 '17 at 12:12

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.