Problem/Motivation

While investigating another issue (#2535422: Alternative text disappears on preview) I stumbled across this bug.

Steps to recreate:

  1. Go to /node/add/article
  2. Fill in the 'Title' field
  3. Hit the 'Preview' button
  4. Hit the 'Back to content editing' button
  5. Hit the 'Preview' button
  6. Fatal Error

Fatal error: Call to a member function getEntityTypeId() on a non-object in /var/www/site/docroot/core/modules/comment/src/CommentForm.php on line 78

Comments

nlisgo’s picture

Issue summary: View changes
biigniick’s picture

i cannot reproduce this on drupal-8.0.0-beta12

nlisgo’s picture

Issue tags: +Regression

Thanks @BiigNiick I confirm also that I cannot reproduce this on drupal-8.0.0-beta12.

nlisgo’s picture

I've tracked down the commit where this bug crept in: #2498919: Node::isPublished() and Node::getOwnerId() are expensive

The last commit sha that didn't have the bug was 9907029cdb30ccc691832865658288af1cfdd25d

nlisgo’s picture

Issue tags: +Needs tests
berdir’s picture

Can you adjust one of the existing tests to trigger this? Not clear to me how the other issue could have broken this, I tried to be very, very careful about keeping BC.

cilefen’s picture

This change should go in PagePreviewTest if anyone is trying.

nlisgo’s picture

Assigned: Unassigned » nlisgo

Attempting to write the test requested in #6.

nlisgo’s picture

Status: Active » Needs review
StatusFileSize
new1.2 KB
nlisgo’s picture

Assigned: nlisgo » Unassigned

The test didn't workout too well because it didn't trigger the error.

Unassigning for now.

nlisgo’s picture

Status: Needs review » Needs work

We need a test which can reproduce the problem described in this issue.

berdir’s picture

That error is in comment.module. So I think it won't happen unless you have comment enabled and a comment field on that node type.

This has to be the check in CommentDefaultFormatter::viewElements(). Somehow in_preview is then empty when it shouldn't be.

I'm guessing this doesn't work anymore the way we it's supposed to:

unset($this->entity->in_preview);

in NodeForm::form().

Try to explicitly change it to set the value to NULL and see if that makes a difference.

Also compare in_preview in the preview() method and the actual controller..

nlisgo’s picture

I will pick this up again tomorrow. Thanks for the pointers @Berdir.

nlisgo’s picture

@Berdir, I should make clear that the reason I believe that the commit #2498919: Node::isPublished() and Node::getOwnerId() are expensive caused this is because after @BiggNiick determined that the bug was not present in drupal-8.0.0-beta12 I found out when the bug first appeared by trial and error (checking out at various commits and reinstalling until I could narrow down, to the exact commit, where the bug was introduced). I'm not clever enough to guess which commit might have broken it :)

I should have some time later today to implement your suggestions on triggering the error in a test only patch and see if your suggestion in #12 fixes it :) Thanks so much.

nlisgo’s picture

Status: Needs work » Needs review
StatusFileSize
new2.76 KB
new1.81 KB
nlisgo’s picture

I just can't seem to break it!

biigniick’s picture

I can confirm this issue is now in beta-13
this is the error i'm getting.
Fatal error: Call to a member function getEntityTypeId() on a non-object in ..../Sites/dru8/core/modules/comment/src/CommentForm.php on line 78

biigniick’s picture

so, i figured i could help narrow it down by uninstalling the comment module but i'm not able to uninstall it with a fresh install... maybe related

it says

Allows users to comment on and discuss published content.
The following reason prevents Comment from being uninstalled:
Fields type(s) in use

next, i did a minimal install so the comment module would not be installed and the same steps create a fatal error with this message

Fatal error: Call to a member function getTranslation() on a non-object in ..../Sites/dru8/core/modules/node/src/NodeViewBuilder.php on line 98

this would suggest the comment module is not the culprit.

- nick

cilefen’s picture

@BiigNiick See #2489472: Field-based module dependency uninstall message is unhelpful and not grammatically correct.

It means there are comment fields on some content types on your install. The message is not helpful, as you have seen.

biigniick’s picture

Ah, thank you @cilefen.

biigniick’s picture

so the error message is in an if statemement that shouldn't even be run

    if (!$is_in_preview)

the problem is probably in the section above where the $is_in_preview is set

  public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) {
    $links = array(
      '#theme' => 'links__node',
      '#pre_render' => array('drupal_pre_render_links'),
      '#attributes' => array('class' => array('links', 'inline')),
    );

also, it works as expected if you use the browser back button. if you click the 'back to content editing' button something is not set right, i would think

on line 42 this must not be setting correct?

            !empty($entity->in_preview),

- nick

biigniick’s picture

StatusFileSize
new596 bytes

it works if you comment this line out.

i know this is not a correct fix, but gets us closer to what's going wrong...

- nick

larowlan’s picture

fail/pass

The last submitted patch, 23: comment-preview-twice-2535792.fail_.patch, failed testing.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks good :)

fabianx’s picture

RTBC + 1

nlisgo’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs beta evaluation

Fantastic. Marking 'Needs work' so we can prepare a beta evaluation.

biigniick’s picture

+++ b/core/modules/node/src/NodeForm.php
@@ -88,7 +88,7 @@ public function form(array $form, FormStateInterface $form_state) {
-      unset($this->entity->in_preview);
+//      unset($this->entity->in_preview);
+++ b/core/modules/node/src/NodeForm.php
@@ -88,7 +88,7 @@ public function form(array $form, FormStateInterface $form_state) {
-      unset($this->entity->in_preview);
+      $this->entity->in_preview = NULL;

YAY! this works for me. I'm glad i could help find where the problem was ;)

the patch from #23 works as expected for me. +1 RTBC and ready for beta evaluation

jibran’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs beta evaluation

It's a major bug so no need for beta eval we can always fix bugs.

nlisgo’s picture

Thanks for the clarification @jibran.

  • webchick committed bdbd7d3 on 8.0.x
    Issue #2535792 by nlisgo, larowlan, BiigNiick: Preview feature broken if...
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Ha, this is timely. :) Just hit this bug a few mins ago while doing a live demo of D8. Fix looks great, as does the test.

Committed and pushed to 8.0.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

granticusiv’s picture

Assuming this is all good now, but noticed this error in 8.0.0-beta14; base install (no additional config or contrib modules), adding the first Article node for the site.