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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIs 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?
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;
}
}
}
hook_node_presave()
:) If you need to mae changes after the node has saved then you want hook_node_update()
and hook_node_insert()