The variable $in_preview is used in the files:

  1. comment.module (lines 587, 676 and 735)
  2. CommentDefaultFormatter.php (line 149)
  3. CommentViewBuilder.php (lines 140 and 171)
  4. CommentLazyBuilders.php (lines 127, 133 and 140)
  5. rdf.module (line 431)

This variable is not declared in the entity Comment and that is not correct.
The entity class Node has also a variable $in_preview and that variable is set public for performance reasons.
I think that Comment::in_preview should do the same.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

daffie’s picture

Status: Active » Needs review
FileSize
784 bytes
andypost’s picture

Issue tags: +Needs beta evaluation

Overall +1 to issue

Would be great to check actual usage of the property and add beta evaluation

+++ b/core/modules/comment/src/Entity/Comment.php
@@ -65,6 +65,17 @@ class Comment extends ContentEntityBase implements CommentInterface {
+   * @var true|null
+   *   TRUE if the comment is being previewed and NULL if it is not.
...
+  public $in_preview = NULL;

suppose bool
default is FALSE

daffie’s picture

I copied the variable $in_preview from the Node entity. Berdir explained in #2525068: Document the class variable Node::in_preview as will stay public comment #3 why variable $in_preview is as it is.

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.

andreyjan’s picture

Comment preview is currently failing with the follwoing error:
Drupal\Core\Entity\EntityMalformedException: The "comment" entity cannot have a URI as it does not have an ID in Drupal\Core\Entity\Entity->toUrl()

Applying #1 patch in combination with the patch from https://www.drupal.org/project/drupal/issues/2914233#comment-12289698 fixes the issue.

This can be also related to https://www.drupal.org/project/drupal/issues/2631246

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.

abhisekmazumdar’s picture

Re-rolled the #1 patch.

andypost’s picture

joachim’s picture

Status: Needs review » Reviewed & tested by the community

LGTM.

catch’s picture

Status: Reviewed & tested by the community » Needs work

#2498919: Node::isPublished() and Node::getOwnerId() are expensive doesn't explain why there's a performance improvement compared to a protected property with a getter method, it just says that there is one. If we're going to commit this just for consistency, I think we should remove the performance claim here. The entity API in 2021 is not the same as the one in 2015 (and neither is PHP itself).

Additionally, the $in_preview property makes comment (and node rendering) tricky with caching.

The whole reason we have this is because of the way node and comment modules render links. We should refactor that code - it would probably need to respect isNew(), but setting a property on the entity is a bad way to pass render context around.

joachim’s picture

> doesn't explain why there's a performance improvement compared to a protected property with a getter method, it just says that there is one

I have to say, I was a bit confused by that too.

#2498919: Node::isPublished() and Node::getOwnerId() are expensive talks about accessing the property causing the flow to go into TypedData. But TypedData only gets invoked for a missing property via the magic __get(). If $node or $comment has the $in_preview property, then isn't that just a straight property access?

catch’s picture

@alexpott pointed out that the magic __get() covers this too - in_preview ends up in a protected property that collects together any random non-field values. So declaring the property would mean that logic doesn't run, but still doesn't seem like much of a performance improvement since this logic is checked very rarely (only to build links).

This issue reminded me how much I'd like to get rid of the underlying logic in general... opened #3244848: Remove comment and node preview.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.