1

Is there a hook responsible for generating the node object's data?

I want to add some custom data to specific node objects to help with the functionality of a very complex website.

Can anyone help, please?

1
  • does't fields allow that? or are you looking at something different? Jan 28, 2013 at 11:51

1 Answer 1

3

hook_node_load() is exactly what you're looking for

Act on arbitrary nodes being loaded from the database.

This hook should be used to add information that is not in the node or node revisions table, not to replace information that is in these tables (which could interfere with the entity cache). For performance reasons, information for all available nodes should be loaded in a single query where possible.

The code example from the docs page is actually very good for your use case so I'll include it here too:

function hook_node_load($nodes, $types) {
  // Decide whether any of $types are relevant to our purposes.
  if (count(array_intersect($types_we_want_to_process, $types))) {
    // Gather our extra data for each of these nodes.
    $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
    // Add our extra data to the node objects.
    foreach ($result as $record) {
      $nodes[$record->nid]->foo = $record->foo;
    }
  }
}
2
  • Thanks for your very helpful answer. I was looking for a way of adding to the node object data at the point creation or being editted
    – sisko
    Jan 28, 2013 at 14:50
  • 1
    Then you're after hook_node_presave() :) If you need to mae changes after the node has saved then you want hook_node_update() and hook_node_insert()
    – Clive
    Jan 28, 2013 at 14:54

Your Answer

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

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