There's a code variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW but no line in readme about this setting

A code in ajax_comments_reply() and ajax_comments_edit() depends on this and throws warnings about undefined $form

Suppose better remove a form with title Add new comment by changin'

  // Delete any extra forms we've created
  //$commands[] = ajax_command_invoke('[id^=comment-wrapper-] form.comment-form', 'remove');
  $commands[] = ajax_command_invoke('.comment-form', 'remove');

Also ajax_comments_reply() does not check for $cid in line

  // Add the new form
  $commands[] = ajax_command_after('#comment-wrapper-' . $cid . ' >.comment', $form);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

inolen’s picture

I'm not really sure about the variable_get() problem, I had rolled that in my patch originally from this patch:
http://drupal.org/node/1243412

As far as the change in ajax_comments_reply(), I'm not sure why you want to do that, that also gets rid of the main reply form at the bottom. Maybe that is desirable, but then you'd need to add a cancel button so you can have that form added back (otherwise once it's removed, it's gone until the user refreshes the page).

When you say check for $cid, do you mean that it's never validated to be an actual comment id?

andypost’s picture

The main problem with comment form is UX mess - you have to have the comment form at the bottom of comments list otherwise everything does not work! So there's no ability to use other values except COMMENT_FORM_BELOW

The proper ways to fix it is allow loading form for reply links and node link "Add new comment" and not depend of visibility of comment form. Also module should cares about existing comment form on the page but not the current way.

About $cid I mean that ajax_comments_reply() checks for reply in

  // If there is a cid this is a reply to a comment.
  if ($cid) {
    $edit = $edit + array('pid' => $cid);
  }

but all other code ignores it, suppose, this $cid is always none-empty because this function is called for reply link so CID is always has value

inolen’s picture

I'll look into it in just a minute.

I've never used the comment form location options.

inolen’s picture

Status: Active » Needs review
FileSize
5.36 KB

I think this should fix everything up for you.

andypost’s picture

Status: Needs review » Needs work

Great!

+++ b/ajax_comments.moduleundefined
@@ -296,7 +329,7 @@ function ajax_comments_reply($nid, $cid) {
+  if ($node->comment == COMMENT_NODE_OPEN) {
     $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);    
     $form = drupal_render($form_build);
   }
@@ -305,7 +338,11 @@ function ajax_comments_reply($nid, $cid) {

@@ -305,7 +338,11 @@ function ajax_comments_reply($nid, $cid) {
   $commands[] = ajax_command_invoke('[id^=comment-wrapper-] form.comment-form', 'remove');
     
   // Add the new form
-  $commands[] = ajax_command_after('#comment-wrapper-' . $cid . ' >.comment', $form);
+  if ($cid) {
+    $commands[] = ajax_command_after('#comment-wrapper-' . $cid . ' >.comment', $form);
+  } else {    ¶
+    $commands[] = ajax_command_append('#comment-wrapper', $form);
+  }

last else should be elsif (isset($form)) {
because we need sure that comments open or admin (UID=1)

andypost’s picture

Status: Needs work » Needs review
FileSize
6.55 KB
13.1 KB
15.97 KB

1278186-ajax-comment-form.patch

This patch fixes hook_menu to mimic more closely comment.module

Also optimizes loading entities by menu. Suppose ajax_comments_reply() should have more checks like http://dgo.to/a/comment_reply does

PS: sorry fro fuzz in patch, current code has a lot of trailing whitespace,

1278186-whitespace.patch cleans up a whitespace problems

1278186-interdiff.txt - contains only changes of code

inolen’s picture

I agree with fixing up the hook_menu() calls for sure (I tried to rip that code out of the reply and delete callbacks), however, why do we need to check with isset($form)?

Should we perhaps just make it early out if $node->comment != COMMENT_NODE_OPEN? I.E. change:

  if ($node->comment == COMMENT_NODE_OPEN) {
    $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);    
    $form = drupal_render($form_build);
  }

to

  if ($node->comment != COMMENT_NODE_OPEN) {
    return;
  }
  
  $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) $edit);    
  $form = drupal_render($form_build);

We shouldn't need to validate the $form based on user permissions at that point.

I'll re-roll the original patch with the user access stuff tonight :)

inolen’s picture

Status: Needs review » Needs work
andypost’s picture

Please take a look at 1278186-interdiff.txt I think return nothing is not a good idea but it works

Also what are you thinking about code-style? suppose module should pass Coder module review

inolen’s picture

I'm not very familiar with what the standard approach is here.

Will attempting to generate a form like you're doing when the node is closed for comments generate something useful, i.e. an error saying "comments are closed" or will it just return an empty form?

andypost’s picture

I think user should get access denied, probably standard message. Currently users are confused by no-reaction from clicking a link

inolen’s picture

Just got home and tested, it looks like the buttons aren't displayed at all when the comments are closed and comment_form_submit() checks for the flag, so I don't think we even need to test for this.

inolen’s picture

Status: Needs work » Needs review
FileSize
5.9 KB

After quickly looking at the permissions, it looks like we do a lot less checking than the normal comments module, that'll have to be worked on in another patch.

Re-rolled the patch, removed the checks for COMMENT_NODE_OPEN because they don't seem to be needed as I said above, and removed some unused globals from the edit function.

We shouldn't add anything else to this patch, but does fix all the issues regarding COMMENT_FORM_BELOW/COMMENT_FORM_SEPARATE_PAGE?

andypost’s picture

Status: Needs review » Needs work

Make sense to split access thing into other issue except hook_menu()

1278186-interdiff.txt

+++ b/ajax_comments.moduleundefined
@@ -13,18 +13,18 @@
-  $items['ajax_comments/edit/%'] = array(
+  $items['ajax_comments/edit/%comment'] = array(
     'page callback' => 'ajax_comments_edit',
-    'access callback' => 'user_access',
-    'access arguments' => array('edit own comments'),
+    'access callback' => 'comment_access',

This part should be in patch

+++ b/ajax_comments.moduleundefined
@@ -316,19 +352,18 @@ function ajax_comments_reply($nid, $cid) {
 function ajax_comments_edit($cid) {
-  global $base_path;
-  global $user;
-  
-  // Load comment and related node  
   $comment = comment_load($cid);
-  $node = node_load($comment->nid);
-  if ( ( (user_access('edit own comments') && $comment->uid == $user->uid ) || user_access('administer comments') )
-      && $node->comment == COMMENT_NODE_OPEN
-      && (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW)) {
-    $form_build = drupal_get_form("comment_node_{$node->type}_form", $comment);
-    $form = drupal_render($form_build);
+  ¶
+  if (!$comment) {
+    return;
   }

Why not just use %comment as menu wildcard as core's comment module does?

+++ b/ajax_comments.moduleundefined
@@ -316,19 +352,18 @@ function ajax_comments_reply($nid, $cid) {
+    ¶

Can you setup your editor to remove trailing whitespaces?

inolen’s picture

Andypost,

I don't think changing just the edit menu item for this patch makes sense.

We can start a new issue and fix reply, edit and delete.

Edit: Doh on the trailing whitespace. New install, need to change that.

andypost’s picture

OK, but a bit of clean-up should be done in this issue

+++ b/ajax_comments.moduleundefined
@@ -13,11 +13,11 @@ function ajax_comments_init() {
-  $items['ajax_comments/reply/%/%'] = array(
+  $items['ajax_comments/reply/%'] = array(
     'page callback' => 'ajax_comments_reply',

This change should be reflected in ajax_comments_reply()

inolen’s picture

What about that isn't reflected in ajax_comments_reply? Should we set a default value of NULL for $cid?

 /**
  *  Callback for clicking "reply".
+ *  Note: $cid 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($nid, $cid) {
andypost’s picture

They should be $node (node object) and $pid = NULL (like core's comment)

inolen’s picture

Went through and made the menus mimic comment's menus, then added the same permissions checks as comment. Also, I changed the 'delivery callback' of the menu items so we don't have to wrap our results in ajax_deliver anymore (was nice for returning error codes).

Downside is, I was an idiot and fixed my trailing whitespace in the middle of this work which turned the patch into an absolute mammoth.

To summarize what was done:
- AJAXify 'add new comment' button to insert comment form on pages where the comment form is not presented by default
- Made comment id in reply menu item optional (^^^)
- Don't add/replace default comment form on reply if one wasn't there by default
- Fixed permissions for reply/edit/delete callbacks
- Made page callbacks used ajax_deliver as the delivery callback

inolen’s picture

Status: Needs work » Needs review
FileSize
7.56 KB

Found the -w flag for git diff, this should be much easier to review :)

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Great! It works and code is much cleaner for now!

let's commit this one (#20) and proceed with white-space clean-up at #1286126: Cleanup code and respect code-style

acouch’s picture

Status: Reviewed & tested by the community » Fixed

thank to both of you for work and patience. i committed the patch at (#20).

andypost’s picture

Great! Now code looks more clean and sane, thanx for taking maintainership

Status: Fixed » Closed (fixed)

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