I have the Organic Groups module working in my site, but when I try to use a simple function like og_load() in my module, I get the following error:

Call to undefined function og_load().

What should I do to use these functions?

up vote 1 down vote accepted

og_load() is defined on the module file, which means it should be available once the module is enabled. Be sure to call the function when Drupal completed its bootstrap, and loaded all the enabled modules. If you call it inside hook_boot(), or hook_exit(), the function could not be available, if Drupal didn't complete its bootstrap.

Sometimes it's enough to increase the weight of the module calling og_load() to avoid the error (if the function is not called in the hooks I previously described). Execute the following code in hook_install(). (Replace mymodule with the short name of your module.)

$weight = db_query("SELECT weight from {system} WHERE name = 'og' AND type = 'module'")->fetchField();
db_update('system')
  ->fields(array('weight' => $weight + 1))
  ->condition('type', 'module')
  ->condition('name', 'mymodule')
  ->execute();
  • Well, the module is enabled, and working, and I'm trying to call the function in a simple hook_preprocess_node. I don't quite get how the weight would affect the access to the functions, but i'll check it up thanks – david ruiz Nov 30 '12 at 12:54

The function is undefined within the scope of your module because your module doesn't know about the Organic Groups module. You have to invoke the Organic Groups module before you can call its functions. (This is the case when using any module's functions.)

Using module_exists() should do the trick. As a bonus, it'll also protect you from unforeseen errors if the Organic Groups module is ever missing or turned off on your site:

if (module_exists('og')) {
  // your og_load() code goes here
}

If you are looking for drupal 7, the api function no more exist, instead node_load($group_id) can be used.

Check this issue link.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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