Where to place PHP code for a node if not in the node body block itself? Up until now, I have found it most convenient and easy to place PHP code into my node by placing it in the node body and enabling the PHP filter. However, I have been informed that this is incorrect and unsafe.

Furthermore, I also know that it would be incorrect to place PHP code in node.tpl.php.

Could someone please tell me where the correct place to place PHP code for a node would be?
One of two places, hook_node_view() or hook_preprocess_node(). They can both be used in a module file, but only the latter will work in your theme's template.php file.

hook_node_view() example:

function MYMODULE_node_view($node, $view_mode, $langcode) {
  if ($node-&gt;type == 'my_node_type') {
    $args = array(':nid' =&gt; $node-&gt;nid);
    $my_value = db_query('SELECT my_field FROM {my_table} WHERE nid = :nid', $args)-&gt;fetchField();

    $node-&gt;content['my_value'] = array(
      '#markup' =&gt; 'My value is: ' . $my_value, 
      '#weight' =&gt; 10 // Controls the display order of this piece of content
    );
  }
}


hook_preprocess_node() example:

function MYMODULE_preprocess_node(&amp;$vars) {
  $vars['content']['my_additional_field'] = array(
    '#markup' =&gt; $additional_field, 
    '#weight' =&gt; 10, 
    '#theme' =&gt; 'mymodule_my_additional_field',
  );
}