Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 246
sisko Avatar asked May 26 '26 08:05

sisko


2 Answers

Use:

require_once DRUPAL_ROOT . '/sites/all/modules/contrib/node_clone/clone.pages.inc';

From the module_load_include API:

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.

like image 91
Free Radical Avatar answered May 30 '26 02:05

Free Radical


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);
}
like image 28
Clive Avatar answered May 30 '26 02:05

Clive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!