1

When I view a node via Services in json or xml format a field I am adding via hook_node_view is unavailable. How could I add the field so it is available also when the node is viewed via Services? I only seem to be able to access the node via hook_node_load

2

hook_node_view() is only invoked when a node is prepared for display (normally either through node_view() or node_view_multiple()).

Since the node isn't prepared for display when you request it via a services call, your hook won't be called. If you think about it this makes perfect sense; you're requesting a json/xml representation of the node so there's absolutely no reason why the node should be prepared for display.

The only way round this would be to move your logic into hook_node_load(), which is called for all node loads. The same data that you attach in hook_node_load() is available to you on the node object in hook_node_view(), so you'll be able to move your logic without any problem at all.

I suppose the other, more complicated method, would be to write your own Services resource that loads the node view as well as the node, so the extra data you're adding would get picked up.

But semantically speaking, unless the actions you're taking relate specifically to the HTML that will be output for the node, hook_node_view() is not the right place to add your extra data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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