5

I want to write a module that makes a select search form based on taxonomy terms. (with hierarchical select boxes) This is on drupal 7.0 and status is up to date. The form is made and Ajax is properly working with testing values. But now I need to find a way to fill up my select boxes with taxonomy terms, wanted to use the function taxonomy_get_tree (modules/taxonomy/taxonomy.module, line 836) and received the message:

Fatal error: Call to undefined function taxonomy_get_tree()

When I've put the function from modules/taxonomy/taxonomy.module in my own module I receive the message:

Fatal error: Cannot redeclare taxonomy_get_tree()

Should it be that I have to hook my module in any kind of way with the taxonomy.module file, or ar there other ways to achieve my goal?

This is on a mymodule.module file placed in drupal7/sites/all/sitename/modules/custom/mymodule/mymodule.module

The code is like

$tree = taxonomy_get_tree(6, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
print_r($tree);

I'm unable to approach all functions in the taxonomy.module page...

4
  • It would be useful to see the code you are using, and know where the code is placed.
    – apaderno
    Apr 28, 2011 at 16:15
  • If you create a sandbox project (drupal.org/node/1068950) other people can look at and collaborate with you on code.
    – rfay
    Apr 29, 2011 at 13:16
  • Sorry, I have to ask: is Taxonomy module enabled?
    – Alex Weber
    May 2, 2011 at 11:04
  • Yes my mistake, forgot to mention that, but it seems to me that I can't call any core module functions at all from my own .module file
    – JurgenR
    May 2, 2011 at 11:52

2 Answers 2

1

If your module depends from another one, and you are getting an error about a function defined from that module not being present, then you should change the weight of your module to a higher value.

In Drupal, modules are loaded basing on their weight (first the lighter modules, and then the heavier modules), and on the short module name; user.module is loaded after taxonomy.module, if both the modules have the same weight.

Instead of using drupal_load('module', 'taxonomy'), you should change the weight of your module in hook_install() or hook_update_N() with db_query("UPDATE {system} SET weight = 10 WHERE name = 'your_module_name'"), or db_update('system')->fields(array('weight' => 1))->condition('name', 'actions_loop_test')->execute() in Drupal 7 and higher.
A better way to alter the weight of your module is to get the weight of the Taxonomy module, increase it of 5 (or 10), and then use that value as weight of your module. In this way, your module hooks would be called after the Taxonomy module is loaded, even in the case a third-party module would change the Taxonomy module weight to a value different from its default.

If the error is caused from the implementation of hook_init(), then you can only use drupal_load('module', 'taxonomy'), as the Taxonomy module doesn't implement hook_init().

1
  • Any value that is higher than the Taxonomy module weight is equally valid; I used 10 as example. By default Drupal core modules have a weight of 0, but there could be some Drupal modules that use a different weight.
    – apaderno
    May 30, 2011 at 20:05
1

Maybe the taxonomy module is just loaded after yours... Have you tried to load the taxonomy module before calling taxonomy_get_tree()? :

drupal_load('module', 'taxonomy');
$tree = taxonomy_get_tree(6, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
print_r($tree);

Your Answer

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

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