Search code examples
drupaldrupal-7drupal-modulescode-reuseinclude-path

How to re-use Drupal module code using module_load_include


I am using the node_clone module with great effect but there is a need in my project to use node_clone functions from my custom module. So I put in the following code:

module_load_include('inc', 'node_clone', 'clone.pages');

function mymodule_init(){
    clone_node_save(118);
}

That code returns Fatal error: Call to undefined function clone_node_save().

My modules are categorized by source into directories labelled mine and contrib. Node_save is in contrib while myModule is in mine.

So, I amended the code acordingly as follows:

module_load_include('inc', '../../contrib/node_clone', 'clone.pages');

but I get the same error.

What am I doing wrong?


Solution

  • It's a bit misleading, the folder is named 'node_clone' but the module is actually called 'clone', so you want:

    module_load_include('inc', 'clone', 'clone.pages');
    

    hook_init() runs pretty early on so if you don't need the clone module's functions before hand, you'd be better off moving the code into the hook:

    function mymodule_init(){
      module_load_include('inc', 'clone', 'clone.pages');
      clone_node_save(118);
    }