3

I have attached a view which displays comments to basic page content type using EVA module. I've also implemented the following to have the "add comment" form displayed just above the comments list:

/**
 * Implements hook_views_pre_build().
 */
function mymodule_views_pre_build(&$view) {
  if ($view->name == 'page_comments') {
    if ($node = menu_get_object()) {
      $comment = new stdClass;
      $comment->nid = $node->nid;
      $form = drupal_get_form('comment_form', $comment);
      $form['#redirect'] = current_path() . '?page=' . (int)$_GET['page'];
      $header = drupal_render($form);
      $view->add_item('default', 'header', 'views', 'area', array('content' => $header, 'format' => 'full_html'));
    }
  }
}

It should work like this:

  1. on page /node/12, which contains the node, comment list and the "add comment" form, the user enters new comment and submits
  2. the page reloads quickly
  3. the submitted comment appears in the comment list on the same page, together with another "add comment" form

What actually happens:

  1. as above
  2. user is redirected to page /comment/reply/12 which also contains the node, comment list and the "add comment" form
  3. however, the previously submitted comment is lost, it doesn't appear in the database or anywhere else
  4. the user can then again enter a comment and submit
  5. if he does so, he is redirected to page /node/12#comment-1561, which contains the node, comment list and the "add comment" form
  6. the second submitted comment is on the comment list

So there are two problems with my added form: comment is not submitted and there is redirection. What needs to be done to make it work the way it should?

1 Answer 1

2

I'm not sure about the failure to submit, but drupal_get_form caches the form config, so making changes to the #redirect after calling it will be lost by the time the form submits. You'd need to use hook_form_alter for that change to persist.

As an alternative, you might consider the AJAX comments module. In theory that will work with a manually embedded comment form, and it'll sidestep your current issue while providing a slightly better UX for comments site-wide.

1
  • I managed to achieve good results with this ajax module, thanks.
    – camcam
    Jun 17, 2016 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.