1

I've installed the Ajax comments module.

I am not able to modify comment form. I want to modify the comment form which one is coming when click reply link.

Note: When I click add new comment the form will show subject, name, comment field. In reply option also showing same form. But I don't want subject and name in reply comment form.

  • More elaboration is needed on the specific problem UR having. – JohnDoea Aug 29 '16 at 6:54
  • @Benia when i click add new comment the form will show subject,name, comment field. in reply option also showing same form. but i don't want subject and name in reply comment form. – udhaya kumar UK Aug 29 '16 at 7:01
  • you can override comment form tpl file – harsh_behl_0007 Aug 29 '16 at 9:43
1

First of all the reply form and the comment form are the same forms (form_id: comment_node_NODE_TYPE_form), you shouldn't think they are different forms.

Second, you would need some custom coding in your own module, that would look like this:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function yourmodule_form_comment_form_alter(&$form, &$form_state, $form_id) {

OR, even more specific to comments on article nodes (you can replace article with page, blog, YOUR_CUSTOM_CONTENT_TYPE)

function yourmodule_form_comment_node_article_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form_state['comment'])) {
    $comment = $form_state['comment'];
    // if this is a reply, hide the two fields
    if($comment->pid > 0) {
      $form['subject']['#access'] = FALSE;
      $form['author']['#access'] = FALSE;
    }
  }
}

Good luck!

  • Using D8 you can check if the form is a reply with : $is_reply = $form_state->getFormObject()->getEntity()->hasParentComment(); – CamelCode May 4 '18 at 8:05
1

I know the pain. I already face the issue.

Just copy the below two functions in an new module and rename the first function name to module name.

Explanation:

Here we just need to remove ' + .indented' in line no 602 & 607. So copy entire function ajax_comments_reply code on to new module This does the trick

Use Menu alter function to modify page callback

function <MODULE_NAME>_menu_alter(&$items) {
// Altering Comment reply function to load form next to reply button
  $items['ajax_comment/reply/%node']['page callback'] = 'ajax_comments_reply_alter';

}

Remove 'indent' when rendering commands ajax_command_append.

 /**
 * Callback for clicking "reply".
 * Note: $pid is an optional parameter. This functionality is utilized by the
 * "Add new comment" link on pages where there is no default comment form
 * (comment_form_location is COMMENT_FORM_SEPARATE_PAGE)
 */
function ajax_comments_reply_alter($node, $pid = 0, $flag = 0) {
  if (!user_access('post comments')) {
    return MENU_ACCESS_DENIED;
  }

  // If there is a pid this is a reply to a comment.
  if (!empty($pid)) {
    if (!user_access('access comments')) {
      return MENU_ACCESS_DENIED;
    }

    // Make sure the comment is valid and published.
    if (!($comments = comment_load_multiple(array($pid), array('status' => COMMENT_PUBLISHED)))) {
      return MENU_NOT_FOUND;
    }
    $comment = $comments[$pid];

    // Make sure the comment belongs to this node.
    if ($comment->nid != $node->nid) {
      return MENU_NOT_FOUND;
    }
  }

  if ((!user_is_anonymous() || (user_is_anonymous() && variable_get('cache', 0) != 0))) {
    // Authenticated user or anonymous and page cache is enabled
    $key = _ajax_comments_cache_key($node->nid, $pid);
    if ($cache = cache_get($key)) {
      // Get form from cache
      $form_build = $cache->data;
    }
    else {
      // Build form and Save to cache*/
      $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid, 'pid' => $pid), array('flag' => $flag));
      cache_set($key, $form_build, 'cache', time() + 360);
    }
  }
  else {
    // Anonymous and page cache is disabled
    $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid, 'pid' => $pid), array('flag' => $flag));
  }

  $form = trim(drupal_render($form_build));

  if (variable_get('ajax_comments_reply_autoclose') && !empty($pid)) {
    $commands[] = ajax_command_remove('.ajax-comments-form-reply');
    $commands[] = ajax_command_invoke('.ajax-comments-reply', 'show');
  }

  // Add the new form.
  if (!empty($pid)) {
    $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
    if (empty($mode)) {
      $commands[] = ajax_command_after('.comment-wrapper-' . $pid, $form);
    }
    else {
      // Add div with class "indented" if they are not exist
      $commands[] = array('command' => 'ajaxCommentsAddDummyDivAfter', 'selector' => '.comment-wrapper-' . $pid, 'class' => 'indented');
      // Check sort by comment_goodness.
      if (_ajax_comments_get_comment_sort_order($node) == 1) {
        // Newer first.
        $commands[] = ajax_command_append('.comment-wrapper-' . $pid, $form);
      }
      else {
        // Older first.
        $commands[] = ajax_command_prepend('.comment-wrapper-' . $pid, $form);
      }
    }
  }
  else {
    // Check Views Add form
    if (!empty($flag)) {
      $commands[] = ajax_command_after('.views-comment-wrapper-nid-' . $node->nid . ' > .ajax-comment-wrapper:last', $form);
    }
    else {
      // Check sort by comment_goodness.
      if (_ajax_comments_get_comment_sort_order($node) == 1) {
        // Older first. Append comment to last wrapper.
        $commands[] = ajax_command_after('.comment-wrapper-nid-' . $node->nid . ' > .ajax-comment-wrapper:last', $form);
      }
      else {
        // Newer first. Append comment to top.
        $commands[] = ajax_command_before('.comment-wrapper-nid-' . $node->nid . '> .ajax-comment-wrapper:first', $form);
      }
    }
  }

  // Hide reply to comment link
  $commands[] = ajax_command_invoke('.ajax-comments-reply-' . $node->nid . '-' . $pid, 'hide');

  if (!variable_get('ajax_comments_disable_scroll', 0)) {
    $commands[] = array('command' => 'ajaxCommentsScrollToElement', 'selector' => '.ajax-comments-reply-form-' . $node->nid . '-' . $pid . '-0');
    $commands[] = ajax_command_invoke('.ajax-comments-reply-form-' . $node->nid . '-' . $pid . '-0 .form-textarea' , 'focus');
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

And Dont forgot to clear cache.

0

There are couple of ways to modify the comment form. For example you can use CSS to hide the elements.

.comment-wrapper .form-item-name { display: none; }

OR place the following in your custom module to remove 'Your Name' from the output

function YOUR_MODULE_form_comment_form_alter(&$form, &$form_state) 
{   
  $form['author']['#access'] = FALSE;
}

or you can override the comment form tpl

  • i don't want to change comment form.i just want to alter when user click reply link it will show comment form i want to modify that one. – udhaya kumar UK Aug 29 '16 at 9:56

Your Answer

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

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