Hello

Bug can be reproduced by following these steps:
1. Create a node.
2. Create a block with PHP input and content (or use any other method to output this code):

  print_r(menu_get_object('node'));

Make sure that block will be shown at your's node.
3. Go to your node.

You should see a printed $node object structure (from block's code). Pay attention that in this object will be a "content" property, which can only be created by node_view(). And it would be not so bad, if "teaser" property been there too. But it's missing. It's removing at node_view().

Solution:

As you may know, menu_execute_active_handler() is passing loaded objects from page_arguments into page_callback. This object lives unchanged until it arrives to node_view():

function node_view($node, $teaser = FALSE) {
  $node = (object)$node;

  $node = node_build_content($node, $teaser, $page);
  ...

As object are passing ONLY by reference, we're getting node_build_content() changes out node object.

The solution of this problem is to clone objects passing to node_view():

function node_view($node, $teaser = FALSE) {
  if (is_object($node)) {
    $node = drupal_clone($node);
  }
  else {
    $node = (object)$node;
  }

  $node = node_build_content($node, $teaser, $page);
  ...

In this case, everyting works perfectly.

Attaching patches for fixing it in D6&7.

CommentFileSizeAuthor
node_D7.patch624 bytesneochief
node_D6.patch498 bytesneochief

Comments

neochief’s picture

P.S. Thanks to MITSUFAN for his help in catching this bug.

neochief’s picture

Status: Active » Needs review
damien tournoud’s picture

Title: node_view() rewrites router items data » node_view() modifies the node object stored in the menu

I'm not quite sure this is a bug and I'm -1 on the solution. We really don't need to use even more memory for this.

neochief’s picture

As alternative, we could not remove teaser and body from node object during node_view (as I see, it's only related to 6x, because in 7x nothing is removed). But I'm not sure if everything will works fine when they will both stay. I'll try to check.

Status: Needs review » Needs work

The last submitted patch failed testing.

catch’s picture

There's a general issue at #154859: Document that cached entity objects are not necessarily cloned any more - does that make this a duplicate?

neochief’s picture

Yes, you're right, it's related to #154859: Document that cached entity objects are not necessarily cloned any more. But I'm not sure that #154859 will get a solution in near future. I think It's too general. Anyway, I can move my patches there if there's a strong need.

sun’s picture

Status: Needs work » Closed (duplicate)
steven jones’s picture

Subscribe.