0

I want to display a comment form in popup in drupal 7 website.

I have created a custom module to display comment form in ctools modal and added a link in the views page. Comment form is displaying correctly in ctools modal but comment is not saving when submitting the form.

When we submit the comment form, it does not display any error and redirect to comment form.

I am also using Ajax comments module in the site.

Code snippets for the ctools

/**
 * Implements hook_menu().
 */
function custom_feature_menu() {

  $items = array();

  $items['modal/%ctools_js/comment-popup/%'] = array(
    'title' => t('Comment Block'),
    'page callback' => 'custom_feature_comment_callback',
    'page arguments' => array(1, 3),
    'access callback' => TRUE,
  );

  return $items;

}


/**
 * Callback for
 */
function custom_feature_comment_callback($js = NULL, $nid = NULL) {

  $node = node_load($nid);
  // setting these defaults is needed, otherwise warnings result in dblog

  if (!$js) {
    return drupal_get_form('comment_node_'.$node->type.'_form', (object)$edit);
  }

  $form_id = 'comment_node_' . $node->type . '_form';

  ctools_include('modal');
  ctools_include('ajax');

  $comment = (object) array('nid' => $node->nid);

  $form_state = array(
    'title' => 'Write a Reivew (' . $node->title . ')',
    'ajax' => true,
  );

  $form_state['build_info']['args'] = array($comment);

  $commands = ctools_modal_form_wrapper($form_id, $form_state);

  if (!empty($form_state['executed'])) {

    $commands = array();

    $commands[] = ctools_modal_command_dismiss(t('Your comment has been saved.'));
    $commands = ctools_ajax_command_reload();
  }

  //If form is submittedd
  print ajax_render($commands);

}

I have searched in google and follow the below reference but comment form is not saving comment.

Thanks in advance.

4
  • Are you aware of the Modal forms module? It support the Comment form out of the box.
    – Chapabu
    Commented Oct 18, 2013 at 12:00
  • Oh, sorry, I knew about it but I thought that it only worked with user pages (login, register, forget password). Thanks, I will look into this. Commented Oct 18, 2013 at 12:03
  • Its only working when we reply to existing comment but not working for 'add new comment'. Commented Oct 18, 2013 at 12:32
  • @Chapabu, I have found the issue. There is action property in comment form which was redirecting to same form when we submit comment form. So, it seems that comment form is not working properly. Commented Oct 21, 2013 at 5:39

1 Answer 1

1

I have found the issue. There is #action property in comment form which was redirecting to same form when we submit comment form. So, it seems that comment form is not working properly.

I have set up a global variable to remove #action property of the comment form if comment form is displaying in ctools popup.

/**
 * Callback for
 */
function custom_feature_comment_callback($js = NULL, $nid = NULL) {

  $node = node_load($nid);
  // setting these defaults is needed, otherwise warnings result in dblog

  global $comment_popup;
  $comment_popup = 1;

  if (!$js) {
    return drupal_get_form('comment_node_'.$node->type.'_form', (object)$edit);
  }

  $form_id = 'comment_node_' . $node->type . '_form';

  ctools_include('modal');
  ctools_include('ajax');

  $comment = (object) array('nid' => $node->nid);

  $form_state = array(
    'title' => 'Write a Reivew (' . $node->title . ')',
    'ajax' => true,
    're_render' => FALSE,
    'no_redirect' => TRUE,
  );

  $form_state['build_info']['args'] = array($comment);

  $commands = ctools_modal_form_wrapper($form_id, $form_state);

  if (!empty($form_state['executed']) || !empty($form_state['ajax_commands'])) {

    $commands = array();

    $commands[] = ctools_modal_command_dismiss(t('Your comment has been saved.'));
    $commands[] = ctools_ajax_command_reload();
  }

  //If form is submittedd
  return ajax_deliver(array('#type' => 'ajax', '#commands' => $commands));

}


function custom_feature_form_alter(&$form, &$form_state, $form_id ) {

  if ($form_id == 'comment_node_article_form') {

    global $comment_popup;

    if ($comment_popup == 1) {

      $comment = $form_state['comment'];

      // If not replying to a comment, use our dedicated page callback for new
      // comments on nodes.
      if (empty($comment->cid) && empty($comment->pid)) {
        // Change the action to call our function.
        $form['#action'] = url('modal/nojs/comment-popup/' . $comment->nid);
      }

      $comment_popup = 0;
    }
  }

}

Your Answer

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

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