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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI 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?
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();
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.