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
Comment | File | Size | Author |
---|
Issue fork drupal-2559833
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
Comment #2
larowlanPart 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.
Comment #3
larowlanComment #4
andypostBlock is not a content entity, so how form is attached?
Comment #5
larowlanI assume it is BlockContent
Comment #6
katzillaTo 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.
Comment #7
larowlanI think this is major
Comment #8
larowlanSimplest fix is to check access.
Comment #9
larowlanComment #12
andypostNice to see more entities tested with comments!
Comment #13
katzillaI 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:
And redirection goes to block/x after action. I think the path needs to be fixed.
Comment #14
andypostHm, 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
Looks that comment form should handle a case to pass "destination" when attached to entity that displayed not at its canonical route
Comment #15
andypostAlso if there's "destination" no need to calculate the page for new comment (tagged performance)
Comment #16
larowlanWe 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.
Comment #17
andypostI still sure better to check that current route is canonical then calculate comment page
if not provide a redirect to current page
Comment #18
larowlanOh, now I get it - good call - will work on this tomorrow
Comment #19
larowlanprodding
Comment #20
larowlanRan out of time, progress patch
Comment #23
katzillaThanks @larowlan for the patch. I think I found an error - uploading a new version.
Comment #26
andypostComment #27
katzillaComment #31
xjmThe 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!
Comment #32
xjmComment #35
sylus CreditAttribution: sylus commentedJust attaching a patch against latest 8.x-5.x-dev based on #27 above.
Comment #36
andypostThe test should be moved to tests/src , the rest looks good
Comment #38
guptahemant CreditAttribution: guptahemant as a volunteer and at QED42 commentedhere is an update patch from #35 following suggestion from #36 ,also fixed some coding standards.
Please review.
Thanks
Comment #39
andypostDo not use deprecated url() method
Tests should be converted according to #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase)
Comment #40
guptahemant CreditAttribution: guptahemant as a volunteer and at QED42 commentedhere is an updated patch according to #39.
Please review.
Thanks
Comment #41
guptahemant CreditAttribution: guptahemant as a volunteer and at QED42 commentedhere is an updated patch deleting the old one
Please review.
Comment #42
andypost@guptahemant please provide interdiff of changes - it makes review much easy
Comment #44
guptahemant CreditAttribution: guptahemant as a volunteer and at QED42 commentedhere is an interdiff between patches in #41 and #38.
Please review.
Comment #46
navneet0693 CreditAttribution: navneet0693 as a volunteer and at QED42 commentedLet's see if test runner is happy. :-)
Comment #48
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedOne more try to make test runner happy =)
Comment #50
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedWe should still redirect after submission in paths like /comment/reply/[entity_type]/[entity_id]/comment so I made some changes to fix this.
Comment #52
piggito CreditAttribution: piggito as a volunteer and at Skilld commentedSetting CommentBlockContentTest to extend from BrowserTestBase instead of CommentTestBase which was causing test to fail.
Also, fixed test fails for comment edition cases.
Comment #56
sjpeters79 CreditAttribution: sjpeters79 commentedUpdated 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.
Comment #57
sjpeters79 CreditAttribution: sjpeters79 commentedUpdated previous patch that was redeclaring the commentedEntity by mistake.
Comment #58
katzillaJust tested the latest patch against 8.7.0-dev and can confirm, that the solution works. I queued the patch to be retested.
Comment #59
katzillaok, the tests still fail. Working on it #ContributionWeekend
Comment #62
yivanov CreditAttribution: yivanov commentedRerolled the above patch for 8.8.x
Comment #63
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedComment #66
JordiK CreditAttribution: JordiK commentedIs 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.
Comment #67
joseph.olstadpatch still applies to 9.1.x
patch still applies to 9.0.x
patch still applies to 8.9.x
Comment #68
andypostInstead of that it could introduce a getCommentedEntity() into separate method but not sure about mentioning "node" in constructor
Comment #69
andypostFixed IS
Comment #71
raman.b CreditAttribution: raman.b at OpenSense Labs commentedComment #72
raman.b CreditAttribution: raman.b at OpenSense Labs commentedResolving failed test cases
#68 still needs to be addressed
Comment #73
joseph.olstadComment #74
joseph.olstadComment #75
golddragon007 CreditAttribution: golddragon007 at European Commission and European Union Institutions, Agencies and Bodies, Petend commentedIn our case, we have the whole content canonical page on a separate URL, and this fixes that case also.
Comment #76
guilhermevp CreditAttribution: guilhermevp at CI&T commentedRe-roled patch #72
Comment #77
golddragon007 CreditAttribution: golddragon007 at European Commission and European Union Institutions, Agencies and Bodies, Petend commentedActually with the reply button the same problem occurs, this patch doesn't fix that case.
And some with edit, delete and any other operation.
Comment #78
vakulrai CreditAttribution: vakulrai as a volunteer and at QED42 for QED42 commented@golddragon007, Added a fix for redirection for rest of the available links on comment form like: delete, reply edit.
Please review.
Comment #80
vakulrai CreditAttribution: vakulrai as a volunteer and at QED42 for QED42 commentedReuploading the updated patch for the failed test in #78, Tests has a deprecated code use case, I have updated that:-
Updated
with ->accessCheck(FALSE).
Comment #82
BhumikaVarshney CreditAttribution: BhumikaVarshney as a volunteer and at OpenSense Labs commentedHi @vakulrai,
Thanks fir the updated patch.
Patch applies cleanly.
RTBC+1
Comment #83
chetanbharambe CreditAttribution: chetanbharambe at QED42 for Drupal India Association commentedComment #84
chetanbharambe CreditAttribution: chetanbharambe at QED42 for Drupal India Association commentedVerified 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.
Comment #85
larowlanThanks 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
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
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()) {
// ...
similarly here
we can use
Url::fromRoute('<current>')->toString()
here and avoid the change to inject the route matchwhy is this change needed?
Comment #86
mohit_aghera CreditAttribution: mohit_aghera at QED42 commentedComment #87
mohit_aghera CreditAttribution: mohit_aghera at QED42 commentedI'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.
Comment #90
golddragon007 CreditAttribution: golddragon007 at European Commission and European Union Institutions, Agencies and Bodies, Petend commentedI 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...
Comment #91
golddragon007 CreditAttribution: golddragon007 at European Commission and European Union Institutions, Agencies and Bodies, Petend commentedA 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.)
Comment #92
Yuri CreditAttribution: Yuri commentedI confirm that patch #87 is working. Using D9.2.6
Comment #93
joseph.olstad#87 still applies (with fuzz) to HEAD of 9.3.x
Comment #94
andypostComment #96
jazzper CreditAttribution: jazzper commentedThanks 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:
And on my web pages with a comment block I get a white screen with:
Comment #97
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedRe-rolling for 9.4.x
Patch was being applied, however it was being applied with fuzz.
Comment #99
mohit_aghera CreditAttribution: mohit_aghera at PreviousNext commentedI 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.
Comment #103
sylus CreditAttribution: sylus commentedJust a re-roll against Drupal 10.2.x
Comment #104
joseph.olstad@sylus, curious to know, which patch did you reroll from? #87 ?
I just triggered the tests for the reroll.
Thanks
Comment #105
joseph.olstadRe-queuing tests for 10.2.x , I did a dry run myself, should be no issue applying. We'll see what the results give.
Comment #106
joseph.olstadThe 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.
Comment #108
joseph.olstad9 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.
Comment #109
smustgrave CreditAttribution: smustgrave commentedLeft some comments on the MR.
Comment #110
smustgrave CreditAttribution: smustgrave commentedRemoved some unused services that were being added. Tests are green thoughts on the change?
Comment #111
smustgrave CreditAttribution: smustgrave at Mobomo commented@joseph.olstad mentioned you've been using this patch for a while. Did the changes in the MR continue to work for you?
Comment #112
joseph.olstad**EDIT**
ignore this comment
**END EDIT**
Comment #113
joseph.olstadah, nevermind, I tried git apply and it went in clean.
Comment #114
joseph.olstadThe merge request applies cleanly against 11.x and 10.3.x but not 10.2.x.
Comment #115
joseph.olstadThanks @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.
Comment #116
alexpottIf 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.
Comment #117
joseph.olstad@alexpott, I responded to your concern and pushed up a new commit. Pipeline is running.
Comment #118
joseph.olstadLOL, reverting my last commit, that code in question actually does do something.
Comment #119
joseph.olstadHmm, 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.
Comment #120
smustgrave CreditAttribution: smustgrave at Mobomo commentedMay also need a rebase? With latest 11.x
Comment #121
joseph.olstadRebased 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:
but the code in the MR is like this:
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.
Comment #122
joseph.olstadah crap, I made a mistake using 10.3.x , sorry folks
Comment #123
joseph.olstadI should have checked that the MR was against 11.x not 10.3.x
Sorry about this. what a mess I just made!
Comment #125
joseph.olstadok @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.
Comment #126
joseph.olstadThe 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
Comment #128
smustgrave CreditAttribution: smustgrave at Mobomo commentedTried rebasing but still getting multiple test failures :(
Comment #129
joseph.olstadCurrent test failures:
1. related to MR 7171
2. This one possibly unrelated(?)