Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
 
does't fields allow that? or are you looking at something different? –  Mohammed Shameem Jan 28 '13 at 11:51
add comment

1 Answer

up vote 2 down vote accepted

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;
    }
  }
}
share|improve this answer
 
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 '13 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 '13 at 14:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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