Problem/Motivation

Views could not be filtered based on a comment field's status of Open, Closed or Hidden. Views were showing every row. An example is a "hidden comments" filter that still shows all comments.

Testing steps

  1. Enable Comment module and add comments to a content type.
  2. Create a node view and add a filter on the comment field's status column. Only the numeric handler is available.
  3. Set the filter to one status value and save.
  4. Preview the view. All nodes are returned regardless of comment status.

Proposed resolution

Use a comment_status field formatter for comment fields that shows the status as an Open, Closed or Hidden label .

Remaining tasks

Create a change record for the deprecation - https://www.drupal.org/node/3628701

User interface changes

  • Views UI comment fields' status filter "Comment status" available with the Open / Closed / Hidden options.
  • "Comment status" formatter is available for comment fields display.

Data model changes

Update hook rewrites saved views to use to the node_comment filter.

Generated with the help of an LLM.

Issue fork drupal-3031364

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mpolishchuck created an issue. See original summary.

mpolishchuck’s picture

Adding the patch.

mpolishchuck’s picture

Status: Active » Needs review

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.

kristen pol’s picture

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

Not sure if this should be considered for core, but patch applies cleanly to 9.1.x.

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.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.

harlor’s picture

Well #2 breaks the current behavior of the comment field. Instead of the list of comments one only gets the comment status in views.

Can't we just add a field formatter as an alternative to CommentDefaultFormatter to show the comment status?

harlor’s picture

StatusFileSize
new1.45 KB

Sorry I just uploaded an incorrect patch.

kristen pol’s picture

Issue tags: +Needs manual testing

Tagging for testing.

kristen pol’s picture

I reviewed the code and it looks very clean :)

joshua1234511’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing
StatusFileSize
new1.48 KB
new20.86 KB

Thank you @harlor, On reviewing the code found the below issue.

diff --git a/modules/comment/src/Plugin/Field/FieldFormatter/CommentStatusFormatter.php b/modules/comment/src/Plugin/Field/FieldFormatter/CommentStatusFormatter.php
...
+++ b/modules/comment/src/Plugin/Field/FieldFormatter/CommentStatusFormatter.php

Patch being applied at incorrect location.
Should be a/core/modules/comment/src/....

Updated the patch to apply at correct path location.

Manually tested the patch, works as expected.

kristen pol’s picture

Status: Reviewed & tested by the community » Needs review

Thanks @joshua1234511 for updating the patch. If someone uploads a patch, then they aren't supposed to RTBC the patch so I'm moving back to needs review so we can get other eyes. I might be able to review today.

kristen pol’s picture

To review the code more carefully, I wanted to see how status is handle in views for other things and here's some things I found:

  1. core/modules/file/src/Plugin/views/filter/Status.php: class Status extends InOperator
      public function getValueOptions() {
        if (!isset($this->valueOptions)) {
          $this->valueOptions = [
            0 => $this->t('Temporary'),
            FileInterface::STATUS_PERMANENT => $this->t('Permanent'),
          ];
        }
        return $this->valueOptions;
      }
    
  2. core/modules/media/src/Plugin/views/filter/Status.php: class Status extends FilterPluginBase
  3. core/modules/node/src/NodeViewsData.php: class NodeViewsData extends EntityViewsData
    public function getViewsData() {
    ...
        $data['node_field_data']['status']['filter']['label'] = $this->t('Published status');
        $data['node_field_data']['status']['filter']['type'] = 'yes-no';
        // Use status = 1 instead of status <> 0 in WHERE statement.
        $data['node_field_data']['status']['filter']['use_equal'] = TRUE;
    ...
    
  4. core/modules/node/src/Plugin/views/filter/Status.php: class Status extends FilterPluginBase
  5. core/modules/user/src/UserViewsData.php: class UserViewsData extends EntityViewsData
    public function getViewsData() {
    ...
        $data['users_field_data']['status']['filter']['label'] = $this->t('Active');
        $data['users_field_data']['status']['filter']['type'] = 'yes-no';
    ...
    

In the comment module currently, there are these filters: NodeComment, StatisticsLastUpdated, and UserUid where NodeComment is currently dealing with status:

/**
 * Filter based on comment node status.
 * 
 * @ingroup views_filter_handlers
 * 
 * @ViewsFilter("node_comment")
 */
class NodeComment extends InOperator {
  
  public function getValueOptions() {
    $this->valueOptions = [
      CommentItemInterface::HIDDEN => $this->t('Hidden'),
      CommentItemInterface::CLOSED => $this->t('Closed'), 
      CommentItemInterface::OPEN => $this->t('Open'),
    ];
    return $this->valueOptions;
  }   
    
}

The patch adds a FieldFormatter (CommentStatusFormatter). I don't see a similar pattern for the File, Media, or Node entities, so I'm not clear this approach would be accepted into core.

kristen pol’s picture

@joshua1234511 Would you please update the issue summary with steps to test this?

joshua1234511’s picture

Issue summary: View changes

I updated the issue summary with testing instructions.

joshua1234511’s picture

@mpolishchuck thinking of the use case, the patch provided adds a new field formatter, that only displays the status and not all details. As far as views are concerned a status field can be easily added to views and the filters can be obtained. the default views show only enabled comments and un approved comments are shown in another tab view.
as the status will be available only for admin, don't see its use for regular users.
and comments are already filters in
.../admin/content/comment
.../admin/content/comment/approval

kristen pol’s picture

Thanks for the clarification @joshua1234511. Since the title and issue summary mentions views, I assumed that the patch would be updating the views filter or field.

Tagging for updating the issue summary to clarify what this new feature really should be.

kristen pol’s picture

Issue summary: View changes

Moved the testing steps higher up on issue summary so they are easier to find.

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.

deepalij’s picture

Assigned: Unassigned » deepalij
anchal_gupta’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new300.11 KB
new107.21 KB

I have applied Patch #14, it was successfully applied and working fine.

deepalij’s picture

Assigned: deepalij » Unassigned
Status: Reviewed & tested by the community » Needs work
StatusFileSize
new102.86 KB
new148.84 KB
new33.83 KB
new726.33 KB
new742.31 KB
new6.48 MB

Successfully applied the patch #14.

Testing Steps followed:
1. Applied the patch
2. Cleared cache
3. Go to /admin/structure/types/manage/article/display
4. Ensure the Comment field options -> Select "comment status" -> Save
5. Now, create content of type Article (for checking the comment status) by visiting /node/add/article
6. Fill up mandatory fields -> Save
7. Observe the error message
8. Go to the Recent log messages and check the error

Expected Result:
Selecting and saving the settings should show the comment status for each comment

Actual Result:
Comment status is available in the manage field
But, to check the comment status on comments when trying to create a node, it's giving the 500 error message as shown in the attached video and in the screenshots

Hence, moving to needs work.

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.

bobooon’s picture

Status: Needs work » Needs review

+1 RTBC for #14 on Drupal 9.5.x. Previously reported errors appear to be related to using the new Olivero theme. Not this patch.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Was tagged in issue summary in #20 which still needs to happen.

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

philltran made their first commit to this issue’s fork.

philltran’s picture

Issue summary: View changes
Status: Needs work » Needs review

I submitted a MR that restores the Views integration from patch #2 on top of current main, and keeps the formatter from patch #14.

The MR now includes an update path. Saved views with a numeric filter on a comment status column are converted to the node_comment filter. Views config saved later with an old numeric filter, such as an imported pre-update export or module-provided config, is converted on save and triggers a deprecation.

Range operators ("<", "<=", ">", ">=", "between", "not between") are converted to an "in" list, because the node_comment filter ignores numeric operators and returns every row.

Generated with the help of an LLM.

philltran’s picture

Updated summary per #20

philltran’s picture

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new1.53 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

philltran’s picture

Status: Needs work » Needs review

cspell was failing on French words in the tests. The cspell tests are passing now. Ready for review.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Kicking back purely because the summary appears to be copy/paste from AI, please see https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquett... summaries need to be own words.

philltran’s picture

Issue summary: View changes

@Smustgrave Thank you for reviewing the issue. I did condense the verbose output from the AI in my summary re-write. I have further edited the summary. Please let me know if I need to may any other changes.

Thanks!

philltran’s picture

Status: Needs work » Needs review