I'm trying to manipulate field values based on a condition evaluated every time a node is loaded. Attempting to affect these values using using hook_node_view() but the changes aren't reflected in the resulting $content array.

Comments

jaypan’s picture

That's because $node->content doesn't exist yet, and is overwritten when it's actually created. It's created in this line:

  $node->content = array();

Which happens in node_build_content(), which in turn is called from node_view().

If you want to add something to $node->content(), you should be doing it in hook_node_view().

Contact me to contract me for D7 -> D10/11 migrations.

zaccb’s picture

I ended up implementing my changes in hook_node_view() but the $content[] array was unaffected.

I had to explicitly call node_build_content() inside the hook.

function mymodule_node_view($node, $view_mode){
    // Code that changes field values

    // Rebuild the $content[] array
    node_build_content($node);
}

Is this already happening in node_view()?

jaypan’s picture

That doesn't make sense, your changes should be reflected. Maybe try this:

function mymodule_node_view(&$node, $view_mode){

Contact me to contract me for D7 -> D10/11 migrations.