I am using Drupal 8.0.0, and I created a tab page for my custom node type (equipped with a field comment) to display just comments, so that comments are not displayed on the node (e.g. node/1) but on tab page (e.g. node/1/comments).

When I add a new comment and click the Save button, the user is redirected to the node (e.g. node/1#comment-16), but I would like to redirect users on the tab page I created (e.g. node/1/comments#comment-16, node/1/comments).

How can I achieve this?

  • are you tried add by destination url – Adi Dec 21 '15 at 5:26
  • If you have the form on separate page you should use destination url argument when redirecting to that form. If that is done via clicking a submit button or of the form is on the same page then you have to alter the comment form, add your own submit handler where you will use $form_state->setRedirect() method. – user21641 Dec 21 '15 at 8:49
  • the tab created by mymodule.routing.yml (using _entity_view on defaults), What I want is: when I click comment save in tab page, redirect to tab page (e.g. node/1/comments), not node view page (e.g. node/1#comment-16). I found $form_state->setRedirectUrl($uri) in core/module/comment/src/commentform.php (line 395), but I don't know how to modify it. Anyone can help me? Thanks! – Time return Dec 23 '15 at 2:58
  • I try to modify line 388: $uri->setOption('fragment', 'comment-' . $comment->id()); to: $uri->setOption('fragment', '/comments'); and add comment and save for test, the URL is localhost/mysite/node/1#/comments, but what I want is localhost/mysite/node/1/comments, I tried again and again but can not remove the "#" in URL. – Time return Dec 23 '15 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.