Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I decide between theme('node', $node) and drupal_render($node->content) for programmatic $node output

Given a $node, I'm trying to decide between the following two ways of outputting that $node.

either

$output = theme('node', $node);

or

node_build_content($node);
$output = drupal_render($node->content);

They both seem to give similar results, but is there something I should consider before I choose one way over the other?

like image 710
dkinzer Avatar asked Oct 08 '10 00:10

dkinzer


2 Answers

Your output is similar if there are no other modules and themes altering the output via the theme layer.

But! If you bypass the theme layer, you'll probably start experiencing unexpected behaviour when you install modules or themes and change config settings that use the theme layer to alter the node's output.

In short, by bypassing the theme layer, you're building error into your application. These error are likely to occur after you hand your application over to someone (a client perhaps) who starts changing settings in admin/

See the decorator pattern if you're interested. Drupal uses this extensively.

http://en.wikipedia.org/wiki/Decorator_pattern

like image 70
Rimian Avatar answered Jan 02 '23 19:01

Rimian


Well, sortof.

What you really want is

$output = node_view($node);

if you just call node_build_content, and then call theme('node', $node), hook_nodeapi('alter') is never called, nor is hook_link().

So if any module is expecting to alter the built node, it wont get a chance to, and if there is supposed to be links on it they wont be there either.

Additionally you can call $output = node_view($node, FALSE, FALSE, FALSE); which gives you the node without the links.

See the documentation for node_view().

like image 41
Mixologic Avatar answered Jan 02 '23 19:01

Mixologic