Problem/Motivation

CommentForm uses $comment->isPublished() to get the default value for the status field on comment edit form.
But this isn't correct as the status fields are integers, not booleans.

Proposed resolution

Add ::getStatus method to CommentInterface and use that for the default value instead of isPublished.

Remaining tasks

Review

User interface changes

None

API changes

new method ::getStatus added to CommentInterface

Original report by @mgifford

In looking at #867830-118: "Unpublished" style of rendered entities is not accessible (and looks bad) I noticed that I couldn't unpublish a comment. The state just didn't stick.

lost state when unpublishing a comment.

Steps to reproduce.

1) Create a article page
2) Add a comment
3) Edit the comment
4) Expand on the Administration link
5) Click Unpublish
6) Save
7) Repeat the process, but stop at step 4 to see example above.

Comments

larowlan’s picture

Issue tags: +Needs tests
thehong’s picture

StatusFileSize
new738 bytes
thehong’s picture

Status: Active » Needs review
mgifford’s picture

Status: Needs review » Needs work
StatusFileSize
new29.39 KB

@thehong - that fixes the problem. Including a screenshot from http://scee2738cdb9c0dc.s2.simplytest.me/comment/1/edit

Usually it should be labelled something like unpublish-comment-2318827-2.patch

But your .diff works fine. Thanks for the patch! Now we just need the tests (I think) till we can mark this RTBC.

larowlan’s picture

/me working on tests

larowlan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new765 bytes
new2.58 KB

Added a test.
Also added ::getStatus to CommentInterface.
Seems counter intuitive to use isPublished to convert status to a bool and then convert it back again.
Thanks @thehong!

larowlan’s picture

Issue summary: View changes

The last submitted patch, 6: comment-status-2318827.fail_.patch, failed testing.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thank you we have test fail which shows it is fixed so RTBC.

andypost’s picture

+++ b/core/modules/comment/src/CommentForm.php
@@ -114,7 +114,7 @@ public function form(array $form, FormStateInterface $form_state) {
-      $status = $comment->isPublished();
+      $status = $comment->getStatus();

I'd prefer do not introduce a new method without test coverage
$status = $comment->isPublished() ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED
should work

larowlan’s picture

Well, the regression test inherently tests the new method.

The reason I added a getStatus method is isPublished does this:

$this->get('status')->value == CommentInterface::PUBLISHED

so if we use isPublished() we're effectively writing this:

$status = ($this->get('status')->value == CommentInterface::PUBLISHED) ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED;

Which I think is inefficient.

Happy to add a unit test for CommentInterface::getStatus if that helps

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I'm not sure a unit test adds much value here. Committed 95019fa and pushed to 8.0.x. Thanks!

diff --git a/core/modules/comment/src/CommentInterface.php b/core/modules/comment/src/CommentInterface.php
index bbf502a..e7a4ee9 100644
--- a/core/modules/comment/src/CommentInterface.php
+++ b/core/modules/comment/src/CommentInterface.php
@@ -216,7 +216,7 @@ public function isPublished();
    * Returns the comment's status.
    *
    * @return int
-   *   One of CommentInterface::PUBLISHED or CommentInterface::UNPUBLISHED
+   *   One of CommentInterface::PUBLISHED or CommentInterface::NOT_PUBLISHED
    */
   public function getStatus();

Fixed on commit.

  • alexpott committed 95019fa on 8.0.x
    Issue #2318827 by larowlan, thehong | mgifford: Fixed Can't unpublish a...
mgifford’s picture

Issue tags: +TCDrupal 2014

In less than 72hrs.. Amazing!

Mostly tagging this so that it can be tracked as part of the sprint.

Status: Fixed » Closed (fixed)

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