Problem/Motivation

Comment form needs to override destination when comment form submitted on comment field attached to block content entity
After submission visitor should stay on the same page

Steps to reproduce

If a comment-form is attached to a block, the redirect after submission is wrong - it points to the page "block/[block-id]/2#comment-x - instead of the current page.

Proposed resolution

add protected method to return commented entity (to which comment is attached to)

Remaining tasks

fix patch, add test, commit

User interface changes

submission a comment form attached to block content entities keeps you on the same page (current URL)

API changes

TBD

Data model changes

no

Release notes snippet

no

CommentFileSizeAuthor
#103 comment_form_redirect-2559833-100.patch14.14 KBsylus
#97 re-roll-diff-2559833-87-97.txt2.52 KBmohit_aghera
#97 comment_form_redirect-2559833-97.patch14.03 KBmohit_aghera
#91 drupal-comment_form_redirect-2559833-91-8.9.x.patch14.3 KBgolddragon007
#87 interdiff-2559833-80-87.txt7.91 KBmohit_aghera
#87 comment_form_redirect-2559833-87.patch13.98 KBmohit_aghera
#87 test-only-2559833-87.patch3.04 KBmohit_aghera
#84 After Patch 2559833 one.png270.76 KBchetanbharambe
#84 After Patch 2559833.png326.79 KBchetanbharambe
#84 Before Patch 2559833.png289.11 KBchetanbharambe
#82 Screenshot 2021-05-03 at 1.28.03 PM.png83.79 KBBhumikaVarshney
#82 Screenshot 2021-05-03 at 1.26.33 PM.png42.84 KBBhumikaVarshney
#80 interdiff-2559833-78-80.txt636 bytesvakulrai
#80 comment_form_redirect-2559833-80.patch11.64 KBvakulrai
#78 interdiff-2559833-76-78.txt5.43 KBvakulrai
#78 comment_form_redirect-2559833-78.patch11.62 KBvakulrai
#76 2559833-76.patch6.19 KBguilhermevp
#72 interdiff_71-72.txt836 bytesraman.b
#72 2559833-72.patch9.54 KBraman.b
#71 interdiff_62-71.txt3.3 KBraman.b
#71 2559833-71.patch8.57 KBraman.b
#62 comment-redirect-2559833-62.patch8.54 KByivanov
#57 comment-redirect-2559833-57.patch8.51 KBsjpeters79
#56 comment-redirect-2559833-56.patch8.51 KBsjpeters79
#52 interdiff-50-52.txt3.61 KBpiggito
#52 2559833-52.patch7.72 KBpiggito
#50 interdiff-48-50.txt4.24 KBpiggito
#50 2559833-50.patch7.32 KBpiggito
#48 interdiff-46-48.txt2.05 KBpiggito
#48 2559833-48.patch6.35 KBpiggito
#46 interdiff-44-46.txt1.42 KBnavneet0693
#46 2559833_46.patch6.34 KBnavneet0693
#44 interdiff.txt2.78 KBguptahemant
#41 comment-form-should-2559833-41.patch6.32 KBguptahemant
#40 comment-form-should-2559833-40.patch19.62 KBguptahemant
#38 comment-form-should-2559833-38.patch6.27 KBguptahemant
#35 comment_form_should-2559833-35.patch6.95 KBsylus
#23 redirect_comment_form-2559833-23.patch6.13 KBkatzilla
#20 comment-redirect-2559833.2.patch6.99 KBlarowlan
#20 interdiff.txt4.21 KBlarowlan
#8 comment-redirect-2559833.pass_.patch4.63 KBlarowlan
#8 comment-redirect-2559833.fail_.patch3.71 KBlarowlan

Issue fork drupal-2559833

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

katzilla created an issue. See original summary.

larowlan’s picture

Part of #1920044: Move comment field settings that relate to rendering to formatter options was to make the redirect path configurable.

I think this means we need it now, rather than later.

larowlan’s picture

andypost’s picture

Block is not a content entity, so how form is attached?

larowlan’s picture

I assume it is BlockContent

katzilla’s picture

To reproduce:
create a custom comment-type for the entity block_content. Then add a comment-field of this type to a custom block-type. Create a block of this type and place it on a page. Write comment and see what happens.

larowlan’s picture

Assigned: Unassigned » larowlan
Priority: Normal » Major

I think this is major

larowlan’s picture

Simplest fix is to check access.

larowlan’s picture

Status: Active » Needs review

The last submitted patch, 8: comment-redirect-2559833.fail_.patch, failed testing.

The last submitted patch, 8: comment-redirect-2559833.fail_.patch, failed testing.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Nice to see more entities tested with comments!

katzilla’s picture

I just tested the patch and can see a different behavior now for anonymous users, but it didn't fix the wrong path.
For example: I have a node/1 with a block on it which is commentable. When I write a comment there I would expect the redirection of the form to be node/1. Now the redirection is e.g. "block/2#comment-4" as a logged in user and "comment/reply/block_content/2/field_comments" as an anonymous.
I just tested the other comment-actions in the block:

  • Delete
  • Edit
  • Reply
  • Approve

And redirection goes to block/x after action. I think the path needs to be fixed.

andypost’s picture

Status: Reviewed & tested by the community » Needs work

Hm, looks #13 pointed the cause - it does not bound to block, we can expose the same state with any entity

For example create a view that displays nodes in full mode with comments, so when comment form submitted user redirected to view page, not commented node

+++ b/core/modules/comment/src/CommentForm.php
@@ -381,6 +382,8 @@ public function save(array $form, FormStateInterface $form_state) {
       // Redirect the user to the entity they are commenting on.
     }
-    $form_state->setRedirectUrl($uri);
+    if ($uri->access()) {
+      $form_state->setRedirectUrl($uri);

Looks that comment form should handle a case to pass "destination" when attached to entity that displayed not at its canonical route

andypost’s picture

Also if there's "destination" no need to calculate the page for new comment (tagged performance)

larowlan’s picture

We could put some alter hooks in block content module to add a new #submit handler for any comment-form that had a comment-type associated with the block_content entity-type.
The #submit handler would redirect back to the previous page.

The issue is that the comment form has an explicit #action.

I don't think comment-form should be aware of nuances of particular entity-types, so it makes sense to be in block_content.module - it can be aware of itself obviously.

andypost’s picture

I still sure better to check that current route is canonical then calculate comment page
if not provide a redirect to current page

larowlan’s picture

Oh, now I get it - good call - will work on this tomorrow

larowlan’s picture

Assigned: Unassigned » larowlan

prodding

larowlan’s picture

Assigned: larowlan » Unassigned
Status: Needs work » Needs review
FileSize
4.21 KB
6.99 KB

Ran out of time, progress patch

Status: Needs review » Needs work

The last submitted patch, 20: comment-redirect-2559833.2.patch, failed testing.

The last submitted patch, 20: comment-redirect-2559833.2.patch, failed testing.

katzilla’s picture

Thanks @larowlan for the patch. I think I found an error - uploading a new version.

Status: Needs review » Needs work

The last submitted patch, 23: redirect_comment_form-2559833-23.patch, failed testing.

The last submitted patch, 23: redirect_comment_form-2559833-23.patch, failed testing.

andypost’s picture

Version: 8.0.x-dev » 8.1.x-dev
katzilla’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 23: redirect_comment_form-2559833-23.patch, failed testing.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now 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.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.

xjm’s picture

Priority: Major » Normal
Issue tags: -Entity Field API

The core committers and entity system maintainers discussed this issue and agreed it can probably be handled as a normal bug per the criteria for major bugs: https://www.drupal.org/core/issue-priority#major-bug

Thanks!

xjm’s picture

Title: Redirect comment form to current page, when form is in block » Comment form should redirect to the current page when the form is in a block

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

sylus’s picture

Status: Needs work » Needs review
FileSize
6.95 KB

Just attaching a patch against latest 8.x-5.x-dev based on #27 above.

andypost’s picture

Status: Needs review » Needs work

The last submitted patch, 35: comment_form_should-2559833-35.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

guptahemant’s picture

Status: Needs work » Needs review
FileSize
6.27 KB

here is an update patch from #35 following suggestion from #36 ,also fixed some coding standards.
Please review.

Thanks

andypost’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/comment/src/CommentForm.php
    @@ -105,7 +112,7 @@ public function form(array $form, FormStateInterface $form_state) {
    +    if ($entity->url() == $this->currentPath->getPath() && !$comment->id() && !$comment->hasParentComment()) {
    
    @@ -397,7 +404,9 @@ public function save(array $form, FormStateInterface $form_state) {
    +    if ($entity->url() == $this->currentPath->getPath() && $uri->access()) {
    

    Do not use deprecated url() method

  2. +++ b/core/modules/comment/tests/src/CommentBlockContentTest.php
    @@ -0,0 +1,104 @@
    +namespace Drupal\comment\Tests;
    ...
    +use Drupal\simpletest\WebTestBase;
    ...
    +class CommentBlockContentTest extends WebTestBase {
    

    Tests should be converted according to #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase)

guptahemant’s picture

Status: Needs work » Needs review
FileSize
19.62 KB

here is an updated patch according to #39.

Please review.

Thanks

guptahemant’s picture

here is an updated patch deleting the old one

Please review.

andypost’s picture

@guptahemant please provide interdiff of changes - it makes review much easy

Status: Needs review » Needs work

The last submitted patch, 41: comment-form-should-2559833-41.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

guptahemant’s picture

Status: Needs work » Needs review
FileSize
2.78 KB

here is an interdiff between patches in #41 and #38.
Please review.

The last submitted patch, 40: comment-form-should-2559833-40.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

navneet0693’s picture

Let's see if test runner is happy. :-)

Status: Needs review » Needs work

The last submitted patch, 46: 2559833_46.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

piggito’s picture

Status: Needs work » Needs review
FileSize
6.35 KB
2.05 KB

One more try to make test runner happy =)

Status: Needs review » Needs work

The last submitted patch, 48: 2559833-48.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

piggito’s picture

Status: Needs work » Needs review
FileSize
7.32 KB
4.24 KB

We should still redirect after submission in paths like /comment/reply/[entity_type]/[entity_id]/comment so I made some changes to fix this.

Status: Needs review » Needs work

The last submitted patch, 50: 2559833-50.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

piggito’s picture

Status: Needs work » Needs review
FileSize
7.72 KB
3.61 KB

Setting CommentBlockContentTest to extend from BrowserTestBase instead of CommentTestBase which was causing test to fail.
Also, fixed test fails for comment edition cases.

Status: Needs review » Needs work

The last submitted patch, 52: 2559833-52.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.

sjpeters79’s picture

Updated the patch to work with latest dev. Also noted that on line 298 of core/modules/comment/src/CommentForm.php, its still using the old entityManager reference. Not sure if that needs to be changed.

sjpeters79’s picture

Updated previous patch that was redeclaring the commentedEntity by mistake.

katzilla’s picture

Just tested the latest patch against 8.7.0-dev and can confirm, that the solution works. I queued the patch to be retested.

katzilla’s picture

ok, the tests still fail. Working on it #ContributionWeekend

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

yivanov’s picture

ravi.shankar’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 62: comment-redirect-2559833-62.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

JordiK’s picture

Is it possible use a destination parameter in the path when commenting on an entity (not replying to another comment)?

Currently if you invoke the new comment form with /comment/reply/{entity_type}/{entity}/{field_name}?destination=someinternalpath, after saving the new comment you get redirected to to the entity commented and destination is not respected.

joseph.olstad’s picture

patch still applies to 9.1.x
patch still applies to 9.0.x
patch still applies to 8.9.x

andypost’s picture

+++ b/core/modules/comment/src/CommentForm.php
@@ -74,12 +92,32 @@ public static function create(ContainerInterface $container) {
+    $this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type.manager');
...
+    $parameters = $route_match->getParameters();
+    if ($parameters->has('node')) {
+      $this->commentedEntity = $parameters->get('node');
+    }
+    elseif ($parameters->has('entity')) {
+      $this->commentedEntity = $parameters->get('entity');
+    }
+    elseif ($parameters->has('comment')) {
+      $comment = $parameters->get('comment');
+      $this->commentedEntity = $this->entityTypeManager
+        ->getStorage($comment->getCommentedEntityTypeId())
+        ->load($comment->getCommentedEntityId());

@@ -117,8 +155,15 @@ public function form(array $form, FormStateInterface $form_state) {
+    if (isset($this->commentedEntity) &&
+      $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() &&
+      $this->commentedEntity->id() == $entity->id() && !$comment->id() &&
+      !$comment->hasParentComment()) {

@@ -409,7 +454,11 @@ public function save(array $form, FormStateInterface $form_state) {
+    if (isset($this->commentedEntity) &&
+      $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() &&

Instead of that it could introduce a getCommentedEntity() into separate method but not sure about mentioning "node" in constructor

andypost’s picture

Issue summary: View changes

Fixed IS

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.

raman.b’s picture

raman.b’s picture

Resolving failed test cases

#68 still needs to be addressed

joseph.olstad’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Issue tags: +ux
golddragon007’s picture

In our case, we have the whole content canonical page on a separate URL, and this fixes that case also.

guilhermevp’s picture

Re-roled patch #72

golddragon007’s picture

Status: Needs review » Needs work

Actually with the reply button the same problem occurs, this patch doesn't fix that case.
And some with edit, delete and any other operation.

vakulrai’s picture

Status: Needs work » Needs review
FileSize
11.62 KB
5.43 KB

@golddragon007, Added a fix for redirection for rest of the available links on comment form like: delete, reply edit.

Please review.

Status: Needs review » Needs work

The last submitted patch, 78: comment_form_redirect-2559833-78.patch, failed testing. View results

vakulrai’s picture

Status: Needs work » Needs review
FileSize
11.64 KB
636 bytes

Reuploading the updated patch for the failed test in #78, Tests has a deprecated code use case, I have updated that:-
Updated

a/core/modules/comment/tests/src/Functional/CommentTestBase.php

with ->accessCheck(FALSE).

 else {
+      $cids = \Drupal::entityQuery('comment')
+        ->accessCheck(FALSE)
+        ->condition('entity_id', $entity->id())
+        ->condition('field_name', $field_name)
+        ->sort('cid', 'DESC')
+        ->range(0, 1)
+        ->execute();
+      if (!empty($cids)) {
+        $cid = reset($cids);
+        \Drupal::entityTypeManager()->getStorage('comment')->resetCache([$cid]);
+        return Comment::load($cid);
+      }
+    }

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.

BhumikaVarshney’s picture

Hi @vakulrai,
Thanks fir the updated patch.
Patch applies cleanly.
RTBC+1

chetanbharambe’s picture

Assigned: Unassigned » chetanbharambe
chetanbharambe’s picture

Assigned: chetanbharambe » Unassigned
Status: Needs review » Reviewed & tested by the community
FileSize
289.11 KB
326.79 KB
270.76 KB

Verified and tested patch #80.
Patch applied successfully and looks good to me.

Testing Steps:
# Go to Structure -> Comment types -> Add comment type - Save it
# Go to Structure -> Add Custom Block -> Save it
# Go to Structure -> Block layout -> Place the created block into any region -> Save it.
# Go to respective page and user should see the comment form.

Expected Results:
# User should be redirected to the current page once adding the new comment.
# User should see Delete, Edit and Reply link under the added comments and that links should be working as expected.

Actual Results:
# Currently User is able to redirect on custom block page which is wrong behaviour.

Can be a move to RTBC.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -redirect, -Performance, -ux +Usability, +Needs tests

Thanks for picking this up again folks.

We need a test here that asserts the new behaviour.

There's quite a lot of work being done here to load the commented entity, when we already have that available as $entity. Here's some suggestions

  1. +++ b/core/modules/comment/src/CommentForm.php
    @@ -76,12 +94,32 @@ public static function create(ContainerInterface $container) {
    +    $parameters = $route_match->getParameters();
    +    if ($parameters->has('node')) {
    +      $this->commentedEntity = $parameters->get('node');
    +    }
    +    elseif ($parameters->has('entity')) {
    +      $this->commentedEntity = $parameters->get('entity');
    +    }
    +    elseif ($parameters->has('comment')) {
    +      $comment = $parameters->get('comment');
    +      $this->commentedEntity = $this->entityTypeManager
    +        ->getStorage($comment->getCommentedEntityTypeId())
    +        ->load($comment->getCommentedEntityId());
    

    this is a comment form.

    it has the scope of the entity (the comment) available as $this->entity

    from there we can determine the commented entity type using $this->entity->entity_type

    We need not hard-code this to be about the 'node' parameter or the 'entity' parameter - and we don't need to use the route match to load the comment, its there as $this->entity

  2. +++ b/core/modules/comment/src/CommentForm.php
    @@ -119,8 +157,15 @@ public function form(array $form, FormStateInterface $form_state) {
    +    if (isset($this->commentedEntity) &&
    +             $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() &&
    +             $this->commentedEntity->id() == $entity->id() && !$comment->id() &&
    +             !$comment->hasParentComment()) {
    

    I think we could simplify this to something like so (pseudo code)

    <?php

    $entity_url = $entity->toUrl();
    if ($entity_url->getRouteName() === $this->routeMatch->getRouteName() && $entity_url->getParameters() === $this->routeMatch->getRawParameters()) {
    // ...

  3. +++ b/core/modules/comment/src/CommentForm.php
    @@ -411,7 +456,11 @@ public function save(array $form, FormStateInterface $form_state) {
    +    if (isset($this->commentedEntity) &&
    +      $this->commentedEntity->getEntityTypeId() == $entity->getEntityTypeId() &&
    +      $this->commentedEntity->id() == $entity->id() && $uri->access()) {
    +      $form_state->setRedirectUrl($uri);
    

    similarly here

  4. +++ b/core/modules/comment/src/CommentLazyBuilders.php
    @@ -165,14 +176,22 @@ public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_pr
    +    $current_route = $this->currentRouteMatch->getRouteName();
    +    $route_options = $this->currentRouteMatch->getRawParameters()->all();
    +    if ($current_route) {
    +      $current_route = Url::fromRoute($current_route, !empty($route_options) ? $route_options : [], ['absolute' => FALSE])->toString();
    +    }
    

    we can use Url::fromRoute('<current>')->toString() here and avoid the change to inject the route match

  5. +++ b/core/modules/comment/tests/src/Functional/CommentTestBase.php
    @@ -183,6 +183,20 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
    +    else {
    +      $cids = \Drupal::entityQuery('comment')
    +        ->accessCheck(FALSE)
    +        ->condition('entity_id', $entity->id())
    +        ->condition('field_name', $field_name)
    +        ->sort('cid', 'DESC')
    +        ->range(0, 1)
    +        ->execute();
    +      if (!empty($cids)) {
    +        $cid = reset($cids);
    +        \Drupal::entityTypeManager()->getStorage('comment')->resetCache([$cid]);
    +        return Comment::load($cid);
    +      }
    

    why is this change needed?

mohit_aghera’s picture

Assigned: Unassigned » mohit_aghera
mohit_aghera’s picture

Assigned: mohit_aghera » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
3.04 KB
13.98 KB
7.91 KB

I've tried to address the issues mentioned in points 1 to 4.
For now, I haven't removed the 5th test case to check that refactoring passes the other test cases.
Refactoring related changes seems to be working fine on local.
Added test only patch as well.

The last submitted patch, 87: test-only-2559833-87.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 87: comment_form_redirect-2559833-87.patch, failed testing. View results

golddragon007’s picture

I thinking of point 4 as that doesn't handle the query parameters (after the ?) and the anchors (#). The first one can be more interesting as a block can be placed on a page with some filters (that usually using query parameters), and then we may need to take care of that. But the anchors, those may not as I think drupal add the new comment's anchor automatically...

golddragon007’s picture

A rollback to 8.9.x of #87 as the new patch doesn't apply to that cleanly. Actually, this patch fixes an issue that we noticed in #76. (On the revision page the site crashes.)

Yuri’s picture

I confirm that patch #87 is working. Using D9.2.6

joseph.olstad’s picture

#87 still applies (with fuzz) to HEAD of 9.3.x

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.

jazzper’s picture

Thanks for the work on the patches!

I'm on Drupal 9.3.6.

Patch #87 applies and works (with fuzz).

Patch #91 does make some changes, but I get the following message in my terminal after applying the patch:

Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2021-06-07/drupal-comment_form_redirect-2559833-91-8.9.x.patch

And on my web pages with a comment block I get a white screen with:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getRouteName() on null in Drupal\comment\CommentForm->form() (line 148 of core/modules/comment/src/CommentForm.php).
Drupal\comment\CommentForm->form(Array, Object) (Line: 106)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 531)
Drupal\Core\Form\FormBuilder->retrieveForm('comment_comments_block_form', Object) (Line: 278)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object) (Line: 123)
Drupal\comment\CommentLazyBuilders->renderForm('block_content', '4', 'field_comments', 'comments_block')
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #lazy_builder callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 772)
Drupal\Core\Render\Renderer->doCallback('#lazy_builder', Array, Array) (Line: 342)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 157)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 158)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 172)
Drupal\Core\Render\Renderer->renderPlaceholder('', Array) (Line: 649)
Drupal\Core\Render\Renderer->replacePlaceholders(Array) (Line: 534)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 145)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 146)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 279)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->renderPlaceholders(Object) (Line: 71)
Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor->processAttachments(Object) (Line: 45)
Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.response') (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 179)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
mohit_aghera’s picture

Re-rolling for 9.4.x
Patch was being applied, however it was being applied with fuzz.

Status: Needs review » Needs work

The last submitted patch, 97: comment_form_redirect-2559833-97.patch, failed testing. View results

mohit_aghera’s picture

I think we might need to review our solution approach.
Here is the scenario that I spotted on local and it seems breaking things.

- Add a few comments on node.
- Click on one of the comment's permalinks. It will change URL like https://drupal8.ddev.site/comment/16#comment-16
- Now when we submit the comment form, page is not loading with new comment. Comment form is also hidden.
- Reason is that, when URL is like comment/{comment_id} neither #actions or form_state->redirect() are being called.

Digging into this further.

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.

joseph.olstad’s picture

@sylus, curious to know, which patch did you reroll from? #87 ?
I just triggered the tests for the reroll.
Thanks

joseph.olstad’s picture

Re-queuing tests for 10.2.x , I did a dry run myself, should be no issue applying. We'll see what the results give.

joseph.olstad’s picture

The test failures appear to be a result of some issue with the ci test runner.

Error found: No space left on device

Perhaps turn this into a merge request and use the gitlab pipeline instead

Unless this is a major regression in the patch, possibly not.

joseph.olstad’s picture

Status: Needs work » Needs review

9 years, we've been using this for likely at least 6 years.

Pleaser review merge request 6622

The WxT distribution is still using this fix, between 200 and 300 installs (likely many more installs that have uninstalled the update module and aren't reporting), multiple public sector organizations, multiple installs in each organization.

smustgrave’s picture

Status: Needs review » Needs work

Left some comments on the MR.

smustgrave’s picture

smustgrave’s picture

@joseph.olstad mentioned you've been using this patch for a while. Did the changes in the MR continue to work for you?

joseph.olstad’s picture

**EDIT**
ignore this comment
**END EDIT**

joseph.olstad’s picture

ah, nevermind, I tried git apply and it went in clean.

joseph.olstad’s picture

The merge request applies cleanly against 11.x and 10.3.x but not 10.2.x.

joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @smustgrave,
Reviewed the code quickly, it's functionally the same as (or very close to) patch 100 except rerolled.

Test coverage is fixed now and all tests are passing.

Also, phpcs seems to be happy with this.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

If I revert all the changes to core/modules/comment/src/CommentForm.php the tests still pass and reading the code I'm not sure why we're making those changes.

joseph.olstad’s picture

@alexpott, I responded to your concern and pushed up a new commit. Pipeline is running.

joseph.olstad’s picture

Status: Needs work » Needs review

LOL, reverting my last commit, that code in question actually does do something.

joseph.olstad’s picture

Hmm, so on "17 February 2024" , the merge request was passing.

I made which seems like a reasonable change to make, the tests fail the failure actually might look like a test bot or ci issue not the actual MR.

Then I reverted the "reasonable" change, same result.

So, it looks like something outside of this merge request changed between February 17th 2024 and today that is affecting the unit tests.

smustgrave’s picture

May also need a rebase? With latest 11.x

joseph.olstad’s picture

Rebased with the latest 10.3.x

I had a look at patch 100, this is the patch that we've been using for several years.

The code in patch 100 is as follows:

@@ -411,7 +448,11 @@ public function save(array $form, FormStateInterface $form_state) {
       $this->messenger()->addError($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]));
       // Redirect the user to the entity they are commenting on.
     }
-    $form_state->setRedirectUrl($uri);
+    $entity_url = $entity->toUrl();
+    if ($entity_url->getRouteName() === $this->routeMatch->getRouteName() &&
+      $entity_url->getRouteParameters() === $this->routeMatch->getRawParameters()) {
+      $form_state->setRedirectUrl($uri);
+    }
   }
 
 }

but the code in the MR is like this:

-    $form_state->setRedirectUrl($uri);
+    $entity_url = $entity->toUrl();
+    if ($entity_url->getRouteName() === $this->routeMatch->getRouteName() &&
+      $entity_url->getRouteParameters() === $this->routeMatch->getRawParameters()) {
+      $form_state->setRedirectUrl($uri);
+    }
+    $form_state->setRedirectUrl($uri);

Somewhere between patch 100 and the current MR, things went weird.

Specifically according to git blame it is the feb 17th commit by smustgrave f522c159ac3a that brought in this change.

So I pushed up dropping the last setRedirectUrl so that the preceding condition becomes once again meaningful.

joseph.olstad’s picture

ah crap, I made a mistake using 10.3.x , sorry folks

joseph.olstad’s picture

I should have checked that the MR was against 11.x not 10.3.x
Sorry about this. what a mess I just made!

joseph.olstad’s picture

ok @smustgrave, I did my best to recuperate your work into the MR 7171 , cherry-picking commits into it.

MR 7171 is based from 11.x

10.3.x was accidentally merged into 6922 by me, sorry.

Should be fixed up with 7171. I believe all of your commits were cherry-picked in.

joseph.olstad’s picture

The current failure is completely unrelated to what we're doing here.

Suspect something is going on with the ci system or maybe a patch that was pushed into 11.x

core/tests/Drupal/Tests/Component/Scaffold/Functional/ManageGitIgnoreTest.php

smustgrave’s picture

Status: Needs review » Needs work

Tried rebasing but still getting multiple test failures :(

joseph.olstad’s picture

Current test failures:

1. related to MR 7171

Exception Other      phpunit-162.xml      0 Drupal\Tests\comment\Functional\Vie
    PHPUnit Test failed to complete; Error: PHPUnit 9.6.15 by Sebastian
    Bergmann and contributors.
    
    Testing Drupal\Tests\comment\Functional\Views\CommentAdminTest
    E.                                                                  2 / 2
    (100%)
    
    Time: 00:24.360, Memory: 4.00 MB
    
    There was 1 error:
    
    1)
    Drupal\Tests\comment\Functional\Views\CommentAdminTest::testApprovalAdminInterface
    Behat\Mink\Exception\ResponseTextException: The text "zsjfmfvf" was not
    found anywhere in the text of the current page.

2. This one possibly unrelated(?)

---- Drupal\Tests\filter\Functional\FilterHtmlImageSecureTest ----
Status    Group      Filename          Line Function                            
--------------------------------------------------------------------------------
Fail      Other      phpunit-172.xml      0 Drupal\Tests\filter\Functional\Filt
    PHPUnit Test failed to complete; Error: PHPUnit 9.6.15 by Sebastian
    Bergmann and contributors.
    
    Testing Drupal\Tests\filter\Functional\FilterHtmlImageSecureTest
    F                                                                   1 / 1
    (100%)
    
    Time: 00:06.514, Memory: 4.00 MB
    
    There was 1 failure:
    
    1)
    Drupal\Tests\filter\Functional\FilterHtmlImageSecureTest::testImageSource
    http://localhost/subdirectory/core/misc/druplicon.png was found.
    Failed asserting that false is true.