I may be doing something wrong, but I cannot get Ajax comments to work in Views. As an example, view the following node for the full node with comments:

http://www.repfun.com/node/184

and this one is now part of a view: http://www.repfun.com/node/185 (with full node view, and comments turned on).

Also, I seem to have to comment out:

//      Drupal.attachBehaviors(html);

at tline 683, or I get a JS error. Interestingly, if no drupal-set-message occurs when the comment is posted, I do not get the error, but if it does I do get an error

Comments

mikerasmussen’s picture

I believe this is related to comment_bonus_api. It appears that when comments are inserted as part of a node in views, it does not end up calling the comment_bonuse_api_comment_render which means the comments are formated with something different, and that the ajax_comments_post_comment_render never gets called, and the javascript is not loaded for the ajax comments....

thaddeusmt’s picture

mikerasmussen pointed out both problems very accurately.

One of the problems is that Views directly outputs the old comments form with comment_render() in template_preprocess_views_view_row_node(). That's easy enough to fix though, just override it in the theme or the ajax_comments module and output comment_bonus_api_comment_render() instead of comment_render(). It was nice of Views to at least put this in a _preprocess_ function!

The OTHER problem is harder to solve though. Many many places in ajax_comments.js jQuery selections of "#comment-form" are hard coded. The problem is that when views outputs the comment forms there could be multiple forms, so it numbers them: "#comment-form-1", "#comment-form-2", etc. The code in ajax_comments.js doesn't detect them so the forms aren't "ajaxified".

I'm not sure how to solve this though. I thought at first I could just change the selector to a wildcard ("[id^=comment-form]" instead of "#comment-form"). This worked for me because I am using Views to just show one node - I don't have multiple comment forms. But if you DID have multiple comment forms, it wouldn't work. You could post from any comment form, but the new comments would appear in the first comments area and all of the ajax stuff like "reply" and "edit" won't work.

Any ideas on how to deal with the dynamic form IDs?

I guess we could do a "foreach" thing and pass in the right form ID as an extra parameter during all of the binds, but that's a bit of refactoring I'm not looking forward to.

Any other ideas?

that0n3guy’s picture

sub...

bflora’s picture

Subscribing.

rjbrown99’s picture

khorpyakov’s picture

StatusFileSize
new39.34 KB

This is a really huge modification (possibly big enought to be a separate version) which fixes ajax_comments compability with views. Almost all the javascript code is rewritten. Fixed comment form id's are replaced with variable id's, allowing multiply forms on the same page.

khorpyakov’s picture

StatusFileSize
new38.44 KB

Removed debug code from the patch above.

jcisio’s picture

Status: Active » Needs work

There is some unrelated files in the patch (like .project), unnecessary coding standards fixes (or introduced problems). Could you submit a new patch with only necessary changes? It's hard to review.

jamesdixon’s picture

khorpyakov this patch is exactly what I'm looking for! I started trying to make the edits you made yourself to get this going with views and multiple comment streams, but my novice javascript skills are holding me back. Then I found this, which is awesome, good job!

Would you mind sending me your patched version, or pointing me in the direction of where I can find the code version you ran the patch on? Was it from CVS revision revision 1.1.2.39? Sorry I'm kind of a noob with patch files, I tried running it on 6.x-1.x-dev but it's been updated since your patch and the effects didn't work.

jamesdixon’s picture

I found the version you patched, and am having difficulties getting it working. It seems like all the form elements are getting processed as they're getting the ajax-processed and ahah-processed classes, but none of the html updates or effects are working. After adding a comment and refreshing the page, the comment appears, so its' kind of working.

Using firebug, I can tell that when I click on "Save" ajax_comments_show_progress isn't getting called. How can I tell if the form elements are getting their functions bound to them properly in Drupal.ajax_comments_init_form?

I also noticed a small bug in the patch at the top of ajax_comments_form_comment_form_alter() in ajax_comments.module:

There were two comment-form-1's being generated with views and multiple comment forms so I updated my code to this:

<?php
function ajax_comments_form_comment_form_alter(&$form, $form_state) {
  // Add class for multiply comments form selection
  $form['#attributes']['class'] = 'comment-form';
  // Change form id for single/multiply comment form processing
  if ($form['#id'] == 'comment-form') {
    $form['#id'] = 'comment-form-1';
    $form_num = 1;
  } else {
    $dash_number = substr($form['#id'], strrpos($form['#id'], '-'));
    $stripped_number = substr($dash_number, 1);
    $form_num = $stripped_number + 1;
  }
...
?>
doublejosh’s picture

Any desire to alter the form ID for multiple forms on a page?
I have the same issue with comments in views. Perhaps that form could use the cid?

doublejosh’s picture

Noticed that fortunately the comment is only displayed as having been submitted to both threads.
After a page refresh the comment is only present in the correct node thread. (D7, not sure about D6.)

nigma3d’s picture

the patch works great, except for when the comment is submitted, the comment doesn't display and refresh via ajax, you have to reload the page to see the comment -- after really digging, it apparently doesn't get the nid or form_num information from ajax, most particularly the function jQuery.fn.ajaxCommentsSubmitToggle

  var obj = $(this[0]);      
  var html = obj.html();
  var link = obj.find('a[href*=/comment/reply]');
  var form_num = ajax_comments_get_form_num_from_href(link.attr('href'));
  var nid = ajax_comments_get_nid_from_href(link.attr('href'));

it fails on the link variable when trying to look through the html var. Dumping the variables show that obj and html work just fine, it just fails on the link variable thus the remainder of the script doesn't work properly. Hardcoding var form_num and var nid for testing makes the script work perfectly (which obviously isn't a solution.

Any idea how to rectify it? I am very inexperienced with jquery.

nigma3d’s picture

actually, found the problem. Changing

var link = obj.find('a[href*=/comment/reply]');

to this:

var link = $(this[0]).find('.comment_reply a').attr('href');

Makes this patch work just fine!

lewie6’s picture

please can someone post a download to the patched module?

or add the code to a dev release?

Berliner-dupe’s picture

When i patch ajax_comment with patch from #8 over Cygwin (windows) i get errors

Hunk #1 FAILED at 110.
Hunk #2 FAILED at 142.
Hunk #1 FAILED at 157.
Hunk #1 FAILED at 167.

Can anyone help me?

El Capitan’s picture

#17, try commit d674cb9574de6c487c161c6fc2d838cad7f5237f from GIT repo, this should be patchable....

SlayJay’s picture

Is there a patch for D7? running into this exact same problem. Need to show a view of comments with ajax functionality.

SlayJay’s picture

FYI, this may be completely unrelated, but I wanted to put it out there in case anyone else runs into this problem.

In D7, if you're trying to make a view of nodes with an ajax pager, and those nodes are embedding a view of comments in their TPL file, and you're using ajax comments... what happens is the ajax comments will work for each of the embedded views, but the main outer view of nodes will have it's ajax functionality broken.

Commenting out the following lines in ajax_comments.module fixes this, with (seemingly) no side effects to ajax comments functionality... at least to my install:

// add 'new' anchor if needed
    if (!empty($comment->first_new)) {
      $prefix .= "<a id=\"new\"></a>\n";
    }

Hope this helps someone!

kmajzlik’s picture

This works for me in Drupal 7:

template.php:

function THEME_preprocess_comment_wrapper(&$variables) {
  $variables['content']['comments']['#prefix'] = '<div id="comments-wrapper-nid-' . $variables['node']->nid . '">';
  $variables['content']['comments']['#suffix'] = '</div>';
}

ajax_comments.module line 231:

    $commands[] = ajax_command_append('#comments-wrapper-nid-' . $form_state['values']['nid'], $notify_text . $comment_output);
Bitvark’s picture

#22 works for me too on D7.
Thanks

jon pugh’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Assigned: Unassigned » jon pugh
Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new1.39 KB

Attached is a patch file with the code from #22, but with ajax_comments_preprocess_comment_wrapper()

Tested on node pages too, works great.

IWasBornToWin’s picture

Is the latest patch in d7 download? I just installed and am having same issue....having to reload page to see comment

tevih’s picture

Having same issue - is this slated to be released?

adamgerthel’s picture

#26 fixed our issues

Update: It doesn't work together with view ajax pager
Update 2: It does work with ajax pager, just not Views Load More module

acouch’s picture

Status: Needs review » Closed (duplicate)
heretic381’s picture

It works, except for the option if you dont want the reply form to be open by default. When you click on "add new comment" link, nothing happens.
Maybe few more lines for that to work?

nikkubhai’s picture

Status: Closed (duplicate) » Needs review

not a duplicate...the other issue is related to panels.

nikkubhai’s picture

marked http://drupal.org/node/1711476 as duplicate of this

grasmash’s picture

I created a completely separate patch for a views "add comment" field here: #1711476: Views integration

It does not modify the module's javascript at all.

mstef’s picture

The problem I'm having is how ajax_comments adds markup to the #prefix and #suffix of comments in hook_comment_view_alter(). It adds numerous open and closed DIV tags, which screw up the layout since Views outputs rows as DIVs. Any one else experiencing that?

This seems only get messed up when we're dealing with comments that have a parent comment.

I'm seeing some #prefix that contain two divs, a close and an open div. Some #suffix contain two closed divs, etc. There's no way that this won't break views, if you're using the Comment display.

To get indenting working with comments, I've been using this:

/**
 * Implements hook_preprocess_views_view_row_comment().
 */
function hook_preprocess_views_view_row_comment(&$vars) {
  $comment = '';
  // The thread sort contains a period for each level deep this comment is.
  // Extract the number of levels by counting the periods.
  $count = substr_count($vars['row']->comment_thread, '.');
  // Add left padding for each level
  for ($x = 0; $x < $count; $x++) {
    $comment .= '<div class="indented">';
  }
  // Add the comment
  $comment .= $vars['comment'];
  // Close the padding for each level
  for ($x = 0; $x < $count; $x++) {
    $comment .= '</div>';
  }
  // Inject the update comment
  $vars['comment'] = $comment;
}

Obviously you need to be using the comment:thread sort (ascending), or you'll have to update your hook.

So, I think ajax_comments needs to handle indenting/wrapping with each individual comment, and stop trying to group them together with DIVs using prefix/suffix.

mstef’s picture

The reason why the ajax_comment indenting isn't working is because it's applying the class tag twice to the wrapper in ajax_comments_comment_alter() (line 377). You end up with:

<div class="ajax-comment-wrapper" id="comment-wrapper" class="indented">

Here's the issue: #1792734: Indentation fix for threaded comments

mstef’s picture

Status: Needs review » Needs work

All of the indenting should probably take place at the end of hook_comment_view_alter(). Instead of nesting each level together, just indent each comment by itself, based on how deep it is:

for ($x = 0; $x < $build['#comment']->depth; $x++) {
  $build['#prefix'] = '<div class="indented">' . $build['#prefix'];
  $build['#suffix'] .= '</div>';
}

And get rid of the $comment->wrappers_to_close, etc...

Heihachi88’s picture

#15 (nigma3d) Are you doing that on drupal 6? Because i changed that line but still it doesn't refresh automatically. I have to reload the page to see a new comment.

ashwinsh’s picture

#22 works for me too on D7.
Thanks

socialnicheguru’s picture

Issue summary: View changes

can #22 be rerolled? it doesn't apply anymore

patch -p1 < ajax-comments-multiple-per-page-743040-comment-26.patch

can't find file to patch at input line 7
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/sites/all/modules/contrib/ajax_comments/ajax_comments.module b/sites/all/modules/contrib/ajax_comments/ajax_comments.module
|old mode 100644
|new mode 100755
|index bdbbaad..79074ff
|--- a/sites/all/modules/contrib/ajax_comments/ajax_comments.module
|+++ b/sites/all/modules/contrib/ajax_comments/ajax_comments.module
--------------------------
File to patch: ./ajax_comments.module
patching file ./ajax_comments.module
Hunk #1 succeeded at 145 (offset 68 lines).
Hunk #2 FAILED at 234.
1 out of 2 hunks FAILED -- saving rejects to file ./ajax_comments.module.rej
aegir@dev:~/p/7/m/all/ajax_comments$ more *rej
--- sites/all/modules/contrib/ajax_comments/ajax_comments.module
+++ sites/all/modules/contrib/ajax_comments/ajax_comments.module
@@ -234,7 +242,7 @@
   else {
     // append comment to root comment wrapper
     $comment_output = drupal_render($comment_build);
-    $commands[] = ajax_command_append('#comment-wrapper', $notify_text . $co
mment_output);
+    $commands[] = ajax_command_append('#comments-wrapper-nid-' . $form_state
['values']['nid'], $notify_text . $comment_output);
 
     // If we have a default form, update it with a new one
     if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BE
LOW) == COMMENT_FORM_BELOW) {
muschpusch’s picture

There is views integration but i don't think it's coming from this issue...

formatC&#039;vt’s picture

Status: Needs work » Fixed

fixed in latest dev

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.