When a comment is submitted, any pager links rendered in the comment field will use the ajax path instead of the path of the node/entity that the comment field is attached to.
Steps to reproduce:
1. Create a node with comment field, ajax comments enabled, set comments per page in the field settings to 10, add 10 comments.
2. Add an 11th comment, observe that the comment is successfully submitted, the comment field is re-rendered via AJAX and has a pager.
3. Observe that the pager links are incorrect.
There are 2 parts to this issue. The first is that when the render array for the comment field is created in renderCommentField, the pager uses the default #route_name, which defaults to , using the relative path from which the pager is rendered. Because this is called within AJAX, it's using the AJAX path instead of the path of the parent node.
The second part of this issue has to do with unnecessary parameters that are being tacked on to the pager links when the pager is rendered within buildCommentFieldResponse (the render array is rendered to an HTML string when it is passed to create a new ReplaceCommand, AjaxCommentsController.php, line 157).
The parameter `_wrapper_format=drupal_ajax' attached to pager links breaks them.
Comments
Comment #2
danmuzyka commentedHey Ryan, thanks for posting this!
Thanks for adding these detailed comments! Could you reformat them so that no line exceeds 80 characters, per Drupal coding standards?
I still think it would be worth exploring a method of doing this that doesn't rely on string replace.
Also, in general, could you inject the services into the constructor and the
create()method rather than using the procedural\Drupal::service()static method here?Could you inject the services into the constructor and the
create()method rather than using the procedural\Drupal::service()static method here?Comment #3
beachston commentedComment #4
beachston commentedIncluded are patches with
1. (test) Just the updated test case
2. Updated test case and fix
Comment #5
beachston commentedComment #10
danmuzyka commentedHere you are using the type
\Drupal\Core\Routing\Router, but in your constructor you are using\Drupal\Core\Routing\AccessAwareRouter, which actually doesn't extend the other class. The common interface that each of them implement is:However, the method you invoke later in the code is
match(), which is declared inDrupal\Core\Routing\AccessAwareRouterInterface, so I would recommend using that in both your class member declaration and your constructor.Some indentation errors that still need cleanup to match Drupal coding standards.
I'll look at this again once you get the test to pass. Thanks!!
Comment #11
rudranil29 commentedComment #12
rudranil29 commentedComment #13
beachston commentedAttached is an updated patch with the updated test.
Looking at core/includes/pager.inc, it doesn't appear to me that there is any good way to get to that '_wrapper_format=ajax' parameter that breaks the pager before it is rendered to a string, since this happens in template_preprocess_pager and any hooks on the pager would be run after that is called. So this fix still uses str_replace to remove it, but in a preprocess function rather than after the entire comment field render array is rendered to Markup in buildCommentFieldResponse.
I also fixed the hardcoded '/node/' in the renderCommentField function to use the entity to get the Route/Route parameters, so this fix should work for any entity using ajax comments, not just nodes.
Comment #15
beachston commentedFixed some coding standards violations, as well as checking that the entity actually has a path. The test runs fine locally but I think it is getting hung up when a comment is a reply to another comment.
Comment #16
beachston commentedFixed some coding standards violations, as well as empty checks for entity route and pager links
Comment #17
beachston commentedFixed some coding standards violations, as well as empty checks for entity route and pager links
Comment #18
beachston commentedComment #19
beachston commentedFixed some coding standards violations, as well as empty checks for entity route and pager links
Comment #21
beachston commentedUses RouteProviderInterface to retrieve the route instead of getting it directly from the entity URL
Comment #22
beachston commentedComment #23
beachston commentedComment #24
beachston commentedComment #25
beachston commentedAdding a comment since a patch with this comment number was already submitted
Comment #26
beachston commentedAdding a comment since a patch with this comment number was already submitted
Comment #27
beachston commentedThis updated patch (27) should resolve coding standards errors introduced by the previous patch (22)
Comment #28
danmuzyka commentedThis looks good to me!
Comment #30
danmuzyka commentedI've merged this into the
8.x-1.xbranch.Comment #31
beachston commentedAfter running into an instance on a client project where the approved patch still resulted in errors, I've given this issue another look. It seems to me that the using
should work for any entity type except when the ajax comment is a reply to another comment, in which case $entity, as it is passed in the function renderCommentField is the parent comment and not the actual parent entity. This patch adds an exception for comments. I don't know the full implications if comments are added to a custom entity but my assumption is that custom entities are given some default path, i.e. calling $entity->toURL() on a custom entity returns the default entity/entityType/id.
Comment #32
beachston commentedUsing entity->toURL is still causing a test failure on D.O, patch 32 is a copy of the approved patch (27) with the comment reply exception code included
Comment #34
danmuzyka commentedThanks @beachston, I've merged your updated patch into the 8.x-1.x branch.