6

How can I call a function of one module in another module? I want to call uc_stock_adjust() function of uc_stock.module in my own custom module. Is it possible?

Here is my code so far:

function my_module_node_presave($node){ 
  if($node->type == 'add_stock') { 
    $stock_value = $node->field_stock_value['und'][0]['value']; 
    $sku = $node->field_sku['und'][0]['value']; 
    if (module_exists('uc_stock') && function_exists('uc_stock_adjust')) {
      uc_stock_adjust($sku, $stock_value); 
    } 
  } 
}   

I want to add and delete stock of products by using reference uc_stock_adjust(), which is defined in uc_stock.module, in my custom module. But when I try the above code, it doesn't work.

It giving an error, and the error is:

Notice: Undefined variable: sku in my_module_node_presave() (line 29 of \sites\all\modules\my_module\my_module.module)‌​

12
  • 1
    have you tried calling the function in your custom module? You can call the functions from any module. Jan 24, 2013 at 6:30
  • It is possible by a simple function call (as in PHP), but it isn't recommended. As the most module functions (hooks) are called on a certain event automatically by Drupal.
    – AjitS
    Jan 24, 2013 at 6:31
  • iam calling uc_stock_adjust function of uc_stock module in my custom module under node_presave function. But it giving an error, and the error is:
    – aashi
    Jan 24, 2013 at 6:36
  • Notice: Undefined variable: sku in my_module_node_presave() (line 29 of D:\xampp\htdocs\photo\photogallary\sites\all\modules\my_module\my_module.module).
    – aashi
    Jan 24, 2013 at 6:37
  • any solution for the above error: undefinrd variable?
    – aashi
    Jan 24, 2013 at 6:51

3 Answers 3

15

To call the function of one module in another module:

  1. If the function is not in the .module file, call module_load_include()

    //Load node.admin.inc from the node module
    module_load_include('inc', 'node', 'node.admin');
    
  2. Call the function from the other module

    function_you_want_to_call();
    
0
0

Undefined variable: sku means that you have not declared a value for the variable $sku. Have you done debugging to make sure that $node->field_sku['und'][0]['value'] outputs a value?

1
  • yes i had done debugging and it outputs the sku value that i have entered manually and that is: tshirts
    – aashi
    Jan 29, 2013 at 4:50
0

See module_invoke(). This calls a function (hook) in a specified module.

The given hook uc_stock_adjust() can thus be called by: module_invoke('uc_stock', 'adjust');. (Note that this is because the function follows Drupal conventions of module_name_function_name).

If desired additional arguments can also be passed to a function's parameters with: module_invoke('uc_stock', 'adjust', $first_param_to_pass, $second_param_to_pass, $etc);

Your Answer

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

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