By cburschka on
My module needs to access the taxonomy terms attached to a node when it is displayed. I dumped the return value of node_load() in the browser and saw that the terms are in an array under $node->taxonomy.
However, when I dump the node object that is passed to hook_load, I don't see $node->taxonomy anywhere.
So at what point do these terms get loaded into the node object, and how can I trigger it? Or should I just "manually" query {term_node} and load the terms myself?
Comments
Order of Modules
Modules are loaded by weight and filename(by default) so check that the taxonomy_nodeapi() function is executed before your modules *_nodeapi() function is called.
ref: http://api.drupal.org/api/HEAD/function/module_list
nodeapi...
Well, "taxonomy" comes (unsurprisingly) after my module's name in the alphabet.
Also, I'm currently using *_load, not *_nodeapi, since I only work on nodes that belong to this module. Should I use *_nodeapi instead?
Anyway, is there a way to easily change the weight settings, or can I just invoke taxonomy_nodeapi myself without any adverse effects?
hook_load() executes before hook_nodeapi()
Since the taxonomy module uses only the hook_nodeapi() your hook_load() is being executed before the terms are loaded as node_invoke() is executed just before node_invoke_nodeapi() in the node.module.
So I would move your code that depends on terms to your hook_nodeapi().
To get a module to load earlier or later from the normal sequence you will have to change the system.weight value for your modules record in the system table. There is not a UI to make this change so your module will have to do this at installation. I would think that you could look up the weight of the module you depend on then make the necessary calculation for your weight.
you have to load it yourself
The data loaded by hook_nodeapi (load) is not saved in the node until all hooks are called. This means that even if taxonomy is called before you module, the data from the taxonomy module is not yet saved in the node and you cannot access it in your nodeapi hook.
Therefore if you need information about the taxonomy you have to load it yourself. But you can use taxonomy_node_get_terms. This is the same function which the taxonomy module calls. The results are cached, thus there is no database overhead. So you can do it in hook_load of your node or whereever you want without performance penalty.
taxanomy data in the hook_nodeapi()
It is true that the taxonomy data is not available in the hook_nodeapi() during the "load" $op. However the taxanomy data is available in the hook_nodeapi() during $op == view, alter, prepare, validate, submit, update.
The original question was "My module needs to access the taxonomy terms attached to a node when it is displayed."
Invoking taxonomy_nodeapi() "by hand"
Invoking taxonomy_nodeapi() directly has worked - since I only need to see the taxonomy of the node, not change it in any way, it was enough to add this:
--
Aran
thanks! very useful and what
thanks! very useful
and what about the taxonomy labels?
I'm trying to use
I'm trying to use hook_nodeapi to track changes to tax terms on a CCK form. I would like to compare the incoming terms to the outbound ones, and write the changes into a database.
I'm struggling on how/when to compare the two. Am I on the right track with this snippet above? Any help would be appreciated.