3

I am trying to load a specific revision for viewing by the end user. Drupal 7.

I have the following in my .module ;

function mymodule_node_load($nodes, $types) {
  if ($nodes[821]->vid != 5928) {
    $nodes[821] = node_load(821, 5928);
  }
}

function mymodule_node_view($node, $view_mode, $langcode) {
   $node = node_load(821, 5928);
}

If I dpm/var_dump the node after doing this, its perfect and is the correct revision but when it comes to rendering, the page chooses the older revision. So something is happening between these functions and the page rendering. What is it? And how can I stop it?

Thanks in advance, much appreciated.

EDIT Anybody know if there is a hook load order chart somewhere? That would be super handy.

EDIT #2 It seems that even the node.tpl.php has the correct revision but what is displayed is not the correct revision ... See image (body['und'][0]['value'] != what is displayed.) enter image description here

EDIT #3 Ok, so I worked out how to do it (code below) its an awful solution and its because of this that I am going to have a look at the Workbench module because I think it might just do what I need. Thanks for all the help!

/**
 * Implements hook_node_view_alter().
 */
function mymodule_node_view_alter(&$build) {
  $latest_revision = _get_latest_published_revision($build['#node']);

  if ($latest_revision) {
    $build['#node'] = node_load($build['#node']->nid, $latest_revision);
    node_build_content($build['#node']);

    $build['body'] = $build['#node']->content['body'];
  }
}
  • To find out the order of module hooks look a the "system" table, all modules have a weight (default=0) and are executed from lowest to highest weights, in case of equal weights they are sorted alphabetically (ascending) – Alex Weber Mar 11 '11 at 6:26
  • 1
    I don't quite understand what you mean by 'See image (body['und'][0]['value'] != what is displayed.)' - can you elaborate on this? It might be useful if you could post your node.tpl.php and explain what is showing up, compared with what you expect to show up. I am not sure whether etiquette dictates you can post long bits of code here, or whether you're better using pastebin.com or similar and linking to it! – Chris Cohen Mar 11 '11 at 12:13
  • @Chris - What I was saying was that the value of body['und'][0]['value'] is different to what is displayed in the node.tpl.php - which is just the default one with a var_dump($node) at the top. – Christian Mar 13 '11 at 22:21
1

What mongolito404 says is right; hook_node_load() and hook_node_view() are not thought to be used to alter the fields already loaded of the node object.

As the documentation reports, they should be used to add additional information.
The documentation of those hooks contains the following sentences:

This hook should only be used to add information that is not in the node or node revisions table, not to replace information that is in these tables […]. [hook_node_load()]

The module may add elements to $node->content prior to rendering. [hook_node_view()]

If you really want to show a different revision of a node basing on some conditions, you could implement hook_menu_alter() to alter the menu callback that handles node/%node, and replace it with your own menu callback that verifies which revision of the node should be shown, and shows it.

1

I think you have misunderstood the usage and intent of hook_node_load. What you do is changing the revision ID (vid) after the node has been fully loaded. Same for hook_node_view(), you are not supposed to replace $node in there, but add additional content to render to $node->content.

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.