Debugging from #955926: Path not linked to node id after previewing then saving node turned up that when you preview a node that $node->uri['path'] gets set to 'node/' when the node eventually gets passed to node_save()/hook_node_insert(). This is because node_preview() operates directly on the $node reference object, which later gets saved to $form['#node'], and used to build the node object when the 'Save' button is pressed. So node previewing calls node_view(clone $node, 'teaser') which is great, but then just two lines down it calls node_view($node, 'full') - without a clone! And because template_preprocess_node() calls entity_uri() on the node object, but the node does not yet have an ID, we get $node->uri['path'] = 'node/'.

So this messes up any type of module that wants to also use entity_uri() on the $node object during hook_node_insert() or hook_entity_insert()!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
702 bytes
Dave Reid’s picture

Issue tags: +pathauto

Adding pathauto tag as this has to be worked-around for now.

Dave Reid’s picture

Issue tags: -Needs tests
FileSize
948 bytes

Added docs. We don't need tests for this as it's obvious the second call to node_view() should have also been a clone, and it's nearly impossible to write a core test for this.

Dave Reid’s picture

Assigned: Unassigned » Dave Reid

Status: Needs review » Needs work

The last submitted patch, 1025870-node-preview-missing-clone.patch, failed testing.

robbycandra’s picture

subscribe

Dave Reid’s picture

Status: Needs work » Needs review
Issue tags: -pathauto

Status: Needs review » Needs work
Issue tags: +pathauto

The last submitted patch, 1025870-node-preview-missing-clone.patch, failed testing.

james.williams’s picture

subscribe

lotyrin’s picture

We took this clone out in #844388: Taxonomy terms disappear from node preview if previewed more than once, which seems to have been reopened to consider returning it, with a patch to do so.

lotyrin’s picture

I think cloning might be the wrong way to go about this. Preview setting the path seems like errant behavior that should simply be fixed. A previewed node doesn't have a path, so it shouldn't be set.

lotyrin’s picture

The way I interpret this issue is as follows:

node_uri makes an errant assumption that the called node will always have an nid.:

function node_uri($node) {
  return array(
    'path' => 'node/' . $node->nid,
  );
}

A previewed node doesn't have one, so this returns 'node/' which is invalid.

It seems that this should check for $node->nid, and return FALSE if it's not set.

lotyrin’s picture

Ow, there's a whole load of things piled on this assumption though.

Wherever $node->uri gets set doesn't check for an empty uri, even though entity_uri() can theoretically return one.

Also, if we allow uri callbacks to return empty values, entity_uri() needs to catch when that happens. Currently, it'll build an empty array and put 'options' in it, instead of itself returning an empty value.

template_preprocess_node() also assumes that entity_uri() always returns a path, blindly calling url() on the return value to set $node_url.

even node.tpl.php can't handle the case that $node_url is empty or not set, the <a> it creates to link to the url isn't conditional.

Cloning on preview fixes pathauto, and is probably the way to quickly get this done. I might open a 8.x issue for the above concern.

SilviaT’s picture

sub

pillarsdotnet’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Bumping and tagging as per standard policy.

sun’s picture

Not limited to nodes.

Seriously, after thinking through this and all the other related issues filed against core, the only proper solution is to add:

$node->unsaveable = TRUE;

in node_view() and similar functions, and

if (isset($thing->unsaveable)) {
  throw Exception('Dude, you painted over my bike. No bikeshedding here.');
}

in node_save() and similar functions.

webchick’s picture

Priority: Major » Normal

This sounds like an edge case to me. Degrading to 'normal' priority. If this is worth blocking features on D8, please explain why.

mgifford’s picture

Assigned: Dave Reid » Unassigned
Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

As far as I can tell this is outdated due to #1510544: Allow to preview content in an actual live environment

Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.

Thanks!