Node class variables should not be accessed directly. Functions should be use to access the variable. For instance use getDescription() and setDescription($description) for the protected class variable description. For a boolean variable the getter function becomes isVariableName(). In object-oriented programming this is called encapsulation.

Berdir as maintainer of the entity system has stated in comment #6 that defining the class variable $in_preview as a public property allows for a considerable performance improvement. And as $in_preview is the only class variable we have only to document this fact in Drupal core.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task
Issue priority Normal
Unfrozen changes Unfrozen because it only changes documentation

Comments

daffie’s picture

Title: Make the class variables protected for Migration » Make the class variables protected for Node
Status: Active » Needs review
StatusFileSize
new6.12 KB

First try with the class variable $in_preview set to protected.

dawehner’s picture

In case you think this is justifyable, this needs a CR.

I'm quite sure that for example @xjm would push that back.

berdir’s picture

This was done by design in #2498919: Node::isPublished() and Node::getOwnerId() are expensive to document an existing, used property. Defining it as a public property allows for a considerable performance improvement.

I really want to do that to the original property as well, it's better to have a documented public property than one that is not but is still used.

I'm OK with adding methods but I'm against making it protected, that's an API change and we're definitely past the time where we can make API changes just because it's cleaner.

daffie’s picture

StatusFileSize
new1.8 KB

Thanks for you replies dawehner and Berdir.

I have left the variable $in_preview public, because of:

This was done by design in #2498919: Node::isPublished() and Node::getOwnerId() are expensive to document an existing, used property. Defining it as a public property allows for a considerable performance improvement.

The patch now only adds the two getter and setter functions and an extra comment by the definition of the $in_preview variable why it is a public one.

mile23’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/node/src/Entity/Node.php
    @@ -77,6 +77,9 @@ class Node extends ContentEntityBase implements NodeInterface {
       /**
        * Whether the node is being previewed or not.
        *
    +   * The variable is set to public for it will result in a considerable
    +   * performance improvement. See https://www.drupal.org/node/2498919
    +   *
        * @var true|null
        */
       public $in_preview = NULL;
    

    Nice comments added here... But since it's public, this docblock should tell us what value corresponds to what state. Especially since it's not TRUE|FALSE but is instead TRUE|NULL.

    Also, the getter and setter never handle NULL, so is the @var annotation correct in the first place?

  2. +++ b/core/modules/node/src/NodeInterface.php
    @@ -168,4 +168,22 @@ public function setRevisionAuthorId($uid);
    +   * Checks if the node is in preview mode.
    +   *
    +   * @return bool
    +   *   TRUE if the node is in preview mode.
    +   */
    +  public function isInPreview();
    +
    +  /**
    +   * Set if the node in to be viewed in preview mode.
    +   *
    +   * @param bool $in_preview
    +   *   Boolean value to indicate if the node in to be viewed in preview mode.
    +   *
    +   * @return $this
    +   */
    +  public function setInPreview($in_preview);
    

    Again, the interface docs don't match up with the property docs, making no mention of NULL.

    So, basically: Let's find out why that NULL is documented in there, and whether it's erroneous.

    I'm also inclined to say the property should be completely encapsulated as protected, but @berdir has spoken. :-)

daffie’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new533 bytes

@Mile23: Thanks for your review! Let me begin by stating that I fully agree with you. And I have no idea why they those for TRUE|NULL instead of TRUE|FALSE.

I am working on closing the #2016679: Expand Entity Type interfaces to provide methods, protect the properties and for me that is making the variables protected or getting the maintainers approval to let them be public. In the patch there is only the comment added that the variable will stay public and why.

Status: Needs review » Needs work

The last submitted patch, 6: 2525068-6.patch, failed testing.

daffie’s picture

Title: Make the class variables protected for Node » Document the class variable Node::in_preview as will stay public
Status: Needs work » Needs review
StatusFileSize
new563 bytes

Some how there was something wrong with my previous patch?! :(

naveenvalecha’s picture

Issue tags: +Quickfix

RTBC +1 if green. Already discussed about the in_preview to be public in the issue.Do we need update at other place in Documenation ?

berdir’s picture

It is TRUE|NULL for BC.

There is code that's checking it with isset($node->in_preview). That is TRUE if the variable is FALSE.

mile23’s picture

Status: Needs review » Needs work

@Mile23: Thanks for your review! Let me begin by stating that I fully agree with you. And I have no idea why they those for TRUE|NULL instead of TRUE|FALSE.

Yah, that's why we should look it up and find out and then know. :-)

It turns out there are a few places where we see things like if (empty($node->in_preview)), such as this from statistics.module:

  if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {

That's exactly the sort of unreadable junk we're trying to avoid by encapsulating in an object. But... There we are. :-)

Anyway.... That sort of code will work with either FALSE or NULL, since PHP allows that.

So:

Add to the property docblock that $is_preview should be TRUE if the node is being previewed, NULL otherwise.

googletorp’s picture

Status: Needs work » Needs review
StatusFileSize
new557 bytes
new672 bytes

I improved the english a bit in the comment. Otherwise looks good.

#10 + #11 Lets not scope creep this issue.

The issue is about missing documentation, not actually changing the actual usage of the in_preview variable. That should be handle in a separate issue.

googletorp’s picture

Issue summary: View changes
googletorp’s picture

Issue tags: +Documentation
mile23’s picture

Status: Needs review » Needs work

It's not scope creep.

Since we're not doing for-reals encapsulation, we need to document the behavior so that it's clear.

daffie’s picture

Status: Needs work » Needs review
StatusFileSize
new483 bytes
new626 bytes

Adding the line to document better the behavior of the variable $in_preview.

googletorp’s picture

Status: Needs review » Reviewed & tested by the community

#15 I see I misread your comment, my bad and I agree.

Anyways, we added the the description in the docblock, so looks all good now.

mile23’s picture

Cool, thanks. RTBC +1

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed b843b4a and pushed to 8.0.x. Thanks!

Thanks for adding the beta evaluation to the issue summary.

  • alexpott committed b843b4a on 8.0.x
    Issue #2525068 by daffie, googletorp: Document the class variable Node::...

Status: Fixed » Closed (fixed)

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