0

Most (if not all) of Drupal functions accept $node object instead of $nid as argument, like node_type_get_name($node), so we should first write node_load($nid) and send its long output to the functions, whereas they can call the node_load() function themselves and reduce number of developers' codes. What's the reason?

  • 2
    Why would you want to load the node again for every single API function? What an incredible slowdown that would be. Loading the object once and passing it around is definitely a better approach. – tim.plunkett Jul 8 '12 at 19:38
2

I think it's to avoid the repetition of node_load that could occur.

I searched a little in the source and you can see node_type_get_name($node) is called from others functions which also take a $node object argument, node_promote_action for exemple

function node_promote_action($node, $context = array()) {
  $node->promote = NODE_PROMOTED;
  watchdog('action', 'Promoted @type %title to front page.', array('@type' => node_type_get_name($node), '%title' => $node->title));
}

It seems DRY to me. Imagine you gave a nid as argument in node_promote_action :

function node_promote_action($nid, $context = array()) {
  $node = node_load($nid);
  $node->promote = NODE_PROMOTED;
  watchdog('action', 'Promoted @type %title to front page.', array('@type' => node_type_get_name($nid), '%title' => $node->title));
}

And then node_load again in node_type_get_name :

function node_type_get_name($nid) {
  $node = node_load($nid); // repetition
  $type = _node_extract_type($node);
  $types = _node_types_build()->names;
  return isset($types[$type]) ? $types[$type] : FALSE;
}

With nid as argument, each time a node related function (A) need to process some node's properties (often if not always), they would call node_load. This would lead to unnecessary repetition when another node processing function (B) is called inside the first (A), just like in the above example.

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.