Updated: Comment #9

Problem/Motivation

On pages like https://drupal.org/book/export/html/1388470 the string 'Add new comment' is added by the comment module when having a config of COMMENT_FORM_SEPARATE_PAGE.

In #754760: "Add new comment" appears directly above comment form / "post comments" does not work without "access comments" permission this is solved for search_index and search_result but missed the print view mode.

Checking this for Drupal 8 gives similar result but when COMMENT_FORM_BELOW it also prints the whole comment form on the Printer friendly page.

Steps to reproduce

  1. Enable the book module
  2. Create a book
    • Create a book content node 'Book 1'.
    • In 'Book outline' choose 'Create a new book'.
    • Save and publish.
  3. Add a child page
    • Click on link 'Add child page' below 'Book 1'
    • Set title to 'Book Child 1'
    • Make sure to add to book outline. Choose 'Book 1'
    • Save and publish
  4. Check printer friendly
    • Click 'up' then 'Printer-friendly version' link from 'Book 1'
    • Expect to see 'Book 1' + 'Book Child 1' (Bug: no 'Book child 1')
  5. Add comment field
    • Go to /admin/structure/types/manage/book/fields
    • 'Re-use existing field' Comment field
  6. Check for comment form
    • Go back to 'Book 1' and click 'Printer-friendly version' link
    • Check the comment form 'Add new Comment'
  7. Check for comment link
    • Go back to /admin/structure/types/manage/book/fields/node.book.comment
    • Uncheck 'Show reply form on the same page as comments' on
    • Go back to 'Book 1' and click 'Printer-friendly version' link
    • See link 'Add new Comment'
  8. Remove comment form from print display mode
    • Go to /admin/structure/types/manage/book/display
    • Add display mode 'print'
    • Remove comments from 'Print' display on /admin/structure/types/manage/book/display/print
    • Go back to 'Book 1' and click 'Printer-friendly version' link
    • Now re-check 'Show reply form on the same page as comments'. This will 'fix' (no bug) the 'Add comment form' on the printer friendly page.
    • Go back to 'Book 1' and click 'Printer-friendly version' link

Proposed resolution

Add the filter for print view mode similar to search_index and search_result.

Remaining tasks

  • Is it OK to add dependency to Comment module for the only Book Test class or should we write a new Test?
  • Write Drupal 7 tests: yes or no?
  • Backport patch #61 to D7.

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

clemens.tolboom’s picture

Title: Comment link of from are added to print view mode. » Comment link or form is added to print view mode.
clemens.tolboom’s picture

Issue summary: View changes

The last submitted patch, core-comment-print-mode.patch, failed testing.

clemens.tolboom’s picture

Failed due to

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 268435456 bytes) in /var/lib/drupaltestbot/sites/default/files/checkout/core/lib/Drupal/Core/Database/Connection.php on line 336
FATAL Drupal\system\Tests\Common\UrlTest: test runner returned a non-zero error code (255).

So let's first true a re-test.

clemens.tolboom’s picture

core-comment-print-mode.patch queued for re-testing.

The last submitted patch, core-comment-print-mode.patch, failed testing.

clemens.tolboom’s picture

mkdir(): No space left on device
clemens.tolboom’s picture

core-comment-print-mode.patch queued for re-testing.

helmo’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: +affects drupal.org
webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Let's get a test for this.

clemens.tolboom’s picture

Tests related to this issue

  • Drupal\search\Tests\SearchCommentTest.
  • Drupal\comment\Tests\CommentLinksTest.
  • Drupal\book\Tests\BookTest.

So we need a test for Book module with comments enabled. Unfortunately function CommentLinksTest::testCommentLinks() uses TestBase::generatePermutations() which does not seem trivial to define a book + comment test for.

clemens.tolboom’s picture

I add this #1166114: Manage Displays Search Results doesn't manage the display of the search results as I think comments on book pages especially the printer friendly variant is a display mode.

Also #1338858: Include theme print stylesheets in Book printer friendly export makes book/export behavior weird.

Enabling comment module for BookTest and creating comment entity for book module gives

<ul class="links inline"><li class="comment-forbidden odd first last"></li></ul>

so we're good.

clemens.tolboom’s picture

Status: Needs work » Needs review
FileSize
2.14 KB

Attached patch fails for "Add new comment"

I'm not happy with the interdependency with comment module introduced by the patch. Can't we do better by creating a new test class like BookCommentTest?

Status: Needs review » Needs work

The last submitted patch, 13: core-comment-print-mode-2141929-13-fail.patch, failed testing.

clemens.tolboom’s picture

Status: Needs work » Needs review
FileSize
3.93 KB

Attached patch merges the fix from #0 and the test from #13

Status: Needs review » Needs work

The last submitted patch, 15: core-comment-print-mode-2141929-15.patch, failed testing.

clemens.tolboom’s picture

Fails on

DefaultViewRecentComments: Drupal\comment\Tests\Views\DefaultViewRecentComments->testBlockDisplay()

DefaultViewsTest.php: Drupal\views\Tests\DefaultViewsTest->testDefaultViews()

Fatal:

Fatal error: Call to a member function getAttribute() on a non-object in /var/lib/drupaltestbot/sites/default/files/checkout/core/modules/system/lib/Drupal/system/Tests/Common/RenderTest.php on line 769
FATAL Drupal\system\Tests\Common\RenderTest: test runner returned a non-zero error code (255).
clemens.tolboom’s picture

Running the failed tests locally gives no error ... is this a testbot hickup #9999 :(

XREF #2100577: Phase 1 - Decouple book module from menu inc

clemens.tolboom’s picture

Status: Needs work » Needs review
clemens.tolboom’s picture

clemens.tolboom’s picture

Issue summary: View changes
larowlan’s picture

+++ b/core/modules/comment/comment.module
@@ -543,7 +543,7 @@ function comment_entity_view(EntityInterface $entity, EntityDisplay $display, $v
+      elseif ($view_mode != 'search_index' && $view_mode != 'search_result' && $view_mode != 'print') {

Ouch, this is becoming unwieldy. Can't the comment field just be removed from the display settings on the print view mode by the site builder?

Correct me if I'm wrong but the comment functionality isn't added to the book node type by default (ie when the book module is enabled) so the user would have had to add this field. Isn't setting the correct display settings part of that process?

Related #2114887: Maximum nesting level when attaching comment field to User.

clemens.tolboom’s picture

@larowlan I totally agree with the

this is becoming unwieldy

'They' did this for search_index and search_result so I blindly copied it over to print.

larowlan’s picture

What about 'Can't the comment field just be removed from the display settings on the print view mode by the site builder?'

clemens.tolboom’s picture

I will recheck this later on this week. IIRC the <list>comment is always printed.

clemens.tolboom’s picture

clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

@larowlan I configured the 'print' display mode for D8 and feel quit stupid :-/

The 'Add new comment' form disappears as it's field is not displayed.

The link 'Add new comment' still appears.

The book tree for 'print friendly' is broken 'Book Child 1' is not displayed.

I'll try to find the link bug and check Drupal 7 again.

clemens.tolboom’s picture

Issue summary: View changes

Added the Display mode steps.

clemens.tolboom’s picture

Status: Needs review » Needs work

This needs work:

- The 'Add new comment' form can be removed through the 'print' display mode settings
- The 'Add new comment' link is still open. This happens when 'Show comment form on another page'.
- How does this work for Drupal 7?

clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

I've removed the form hiding parts.

In #1166114-107: Manage Displays Search Results doesn't manage the display of the search results I asked for a better solution for hiding the comment link as we are doing something wrong.

If display mode dictates not to show comments it should not display the link either. Should this be a pseudo field providing a link 'Add new comment'?

The code in comment_entity_view() currently is node specific and assumes too much. But it has a TODO refering to #1901110: Improve the UX for comment bundle pages and comment field settings

Same goes for CommentDefaultFormatter::viewElements() I guess. Why isn't this handled by display mode either?

      !in_array($this->viewMode, array('search_result', 'search_index'))) {

I dunno how to solve this so hints are welcome.

clemens.tolboom’s picture

Status: Needs work » Needs review

(darn: I was distracted about preview is not rendering my comment :/)

The last submitted patch, 34: core-comment-print-mode-2141929-33.patch, failed testing.

jhodgdon’s picture

Is there some way we could make this smarter? Because I'm finding we will need to add yet another exception to that line in comment.module in #1166114: Manage Displays Search Results doesn't manage the display of the search results.

It just seems stupid. We should have a better way to manage it.

clemens.tolboom’s picture

Added link to #1901110: Improve the UX for comment bundle pages and comment field settings which has most desired settings in it.

I think we can set version to D7 then.

andypost’s picture

Looks as good workaround with test!
Just a minor nitpicks

  1. +++ b/core/modules/book/lib/Drupal/book/Tests/BookTest.php
    @@ -207,7 +211,7 @@ function checkBookNode(EntityInterface $node, $nodes, $previous = FALSE, $up = F
    +    $this->assertNoText('Add new comment', '"Add new comment" not found');
    

    Should be t('Add new comment')

  2. +++ b/core/modules/comment/comment.module
    @@ -543,11 +543,15 @@ function comment_entity_view(EntityInterface $entity, EntityViewDisplayInterface
    +      elseif ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print') {
    +        // We don't want this link if we're building the entity for:
    

    Suppose better to add 'continue;' here

mgifford’s picture

Andy, what do you mean by "Suppose better to add 'continue;' here"?

mgifford’s picture

Status: Needs review » Needs work

The last submitted patch, 40: core-comment-print-mode-2141929-40.patch, failed testing.

The last submitted patch, 40: core-comment-print-mode-2141929-40.patch, failed testing.

andypost’s picture

The proper fix should remove comment add form too.
@mgifford please check my grammar in comment formatter

PS: Working on test

mgifford’s picture

It is
// We don't want this links if we're building the entity for:

It should be either (if plural links):
// We don't want these links if we're building the entity for:

or (if singular link):
// We don't want this link if we're building the entity for:

Small thing, but:
// display below the entity. Do not show the form for print view mode.

Should be capitalized:
// Display below the entity. Do not show the form for print view mode.

andypost’s picture

Assigned: andypost » Unassigned
Issue tags: -Needs tests
FileSize
1.63 KB
2.42 KB
5.04 KB

Changed code comments a bit, added test. Suppose RTBC

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Assuming bot agrees.
I'm on record above saying I don't like that comment module is aware of search and print view modes, but this is existing in HEAD and this patch solves the issue in the OP, fixing the wider problem is out of scope.
We have existing issues to make the formatter more configurable and I'll try and pick them up again next week.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 46: 2141929-comment-print-46.patch, failed testing.

The last submitted patch, 46: 2141929-comment-print-46-fail.patch, failed testing.

The last submitted patch, 46: 2141929-comment-print-46-fail.patch, failed testing.

mgifford’s picture

Status: Needs work » Needs review

46: 2141929-comment-print-46.patch queued for re-testing.

mgifford’s picture

The last submitted patch, 46: 2141929-comment-print-46-fail.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 46: 2141929-comment-print-46.patch, failed testing.

clemens.tolboom’s picture

Issue summary: View changes

Removed bug from steps to reproduce.

Testbot is buggy as the failed test CommentBookTest.php succeeds on local.

clemens.tolboom’s picture

Status: Needs work » Needs review

46: 2141929-comment-print-46.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 46: 2141929-comment-print-46.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review

46: 2141929-comment-print-46.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 46: 2141929-comment-print-46.patch, failed testing.

The last submitted patch, 46: 2141929-comment-print-46.patch, failed testing.

andypost’s picture

Status: Needs work » Needs review
FileSize
1.17 KB
2.64 KB
5.25 KB

The comment was saved unpublished so run-tests is less affected leaking from host site, this should be fine after #2171683: Remove all Simpletest overrides and rely on native multi-site functionality instead
So now it works.

The last submitted patch, 61: 2141929-comment-book-59-fail.patch, failed testing.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC

larowlan’s picture

+1 RTBC as per #47

jhodgdon’s picture

I noticed that this change:

+++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -143,8 +143,8 @@ public function viewElements(FieldItemListInterface $items) {
       }
 
       // Append comment form if the comments are open and the form is set to
-      // display below the entity.
-      if ($status == COMMENT_OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW) {
+      // display below the entity. Do not show the form for the print view mode.
+      if ($status == COMMENT_OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') {

is only being made for print mode. Can't we also make this for search indexing and search results? It shouldn't be there either. See
#1113832: [PP-1] Comment module renders "reply" and other links in search index/results
which has this marked as a related issue. The other changes in this issue were made for all three, such as:

+++ b/core/modules/comment/comment.module
@@ -436,6 +436,13 @@ function comment_entity_view(EntityInterface $entity, EntityViewDisplayInterface
     //   http://drupal.org/node/1901110
     return;
   }
+  if ($view_mode == 'search_index' || $view_mode == 'search_result' || $view_mode == 'print') {

It would be great if this one could be too. Is it too late to revisit the patch?

jhodgdon’s picture

A suggestion for a test for the search-related behavior:
- enable search module
- make sure anonymous users have permission to post comments (without approval) and search
- make a node on a content type with comments enabled
- run cron to index
- search for the word "reply". If you get a result, then the "reply" link was rendered in the search indexing step and it shouldn't have been.
- search for a word that is in the node body or node title. Verify that the word "reply" is not shown on the screen.

andypost’s picture

@jhodgdon
1) the formatter already checks search view modes just before the fixed code (see line 122)
!in_array($this->viewMode, array('search_result', 'search_index'))) {
and formatter's purpose to display "comment thread and form", so current patch hides the form

2) this is a fix amd a bit of optimization, suppose this covers the issue #1113832: [PP-1] Comment module renders "reply" and other links in search index/results

So patch does not need any changes, also it makes sense to add the test in following issue

andypost’s picture

Related issues:

re-order patches

jhodgdon’s picture

RE #67, oh, right, I see that... well we can work on the test in the other issue and see if it's still a problem after this patch, which I think will not fix the other issue.

The problem on that other issue is actually that the reply link is shown during search indexing and search results display, which is not affected by this patch, I think.

jhodgdon’s picture

So... Let's step back a moment and think about this really kind of kludgy code.

Field formatters can have settings.

So wouldn't it be more sensible rather than special-casing the search_index, search_results, and print display modes, to have settings in the Comment field formatter that say things like:
- If this setting is FALSE, skip rendering the comment form
- If this other setting is FALSE, skip rendering the "add" and "reply" links

This would allow us to remove the ad-hoc code in both of these places... just a thought, but maybe coming too late.

clemens.tolboom’s picture

In #12, #34 and #37 we scratched the 'can we do better'.

iirc the UL tag and content is still on the print-friendly page

Let's fix 'can we do better' through either #1113832: [PP-1] Comment module renders "reply" and other links in search index/results and/or #1166114: Manage Displays Search Results doesn't manage the display of the search results

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +Needs backport to D7

OK I agree this is messy but also that it's OK to fix it with workaround + tests then follow-up with the refactoring. We likely won't be able to backport the refactoring.

Committed/pushed to 8.x, moving to 7.x for backport.

johnv’s picture

Issue summary: View changes

I've marked [#583576s] as a duplicate.
Patch #61 is a D8-patch and needs backporting.

clemens.tolboom’s picture

  • catch committed 6c4cdee on 8.3.x
    Issue #2141929 by andypost, clemens.tolboom, mgifford: Comment link or...

  • catch committed 6c4cdee on 8.3.x
    Issue #2141929 by andypost, clemens.tolboom, mgifford: Comment link or...

  • catch committed 6c4cdee on 8.4.x
    Issue #2141929 by andypost, clemens.tolboom, mgifford: Comment link or...

  • catch committed 6c4cdee on 8.4.x
    Issue #2141929 by andypost, clemens.tolboom, mgifford: Comment link or...