As far as comments order can be changed (by altering query) ajax_comments should have an opportunity to put new comments to the top. So I changed admin settings form and added this feature. Please, take a look on the patch.

Comments

paulhudson’s picture

Manually applying the patch worked like a charm. +1 from me for this functionality.

Unfortunately the patch failed when I applied it to the latest 7.x-1.x-dev so I guess it needs to be recreated but as I say, manually applying it works.

Additionally, it would be nice to have the option of the comment form displaying at the top of the comments.

paulhudson’s picture

To that end, here's the code I'm using to prepend the comment form to the top of the wrapper if comments are ordered newest first:


/**
 * 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($node, $pid = NULL) {
  if (!user_access('post comments')) {
    return MENU_ACCESS_DENIED;
  }

  // If there is a pid this is a reply to a comment.
  if ($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;
    }
  }

  // Build form.
  $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array('nid' => $node->nid, 'pid' => $pid));

  // Offer 'cancel' link for reply. This will just remove the reply form so
  // there is no callback.
  $form_build['actions']['cancel'] = array(
    '#markup' => '<a id=\'ajax-comments-reply-' . $pid . '\' class=\'ajax-comments-reply-cancel\' href="#' . $form_build['#id'] . '">' . t('Cancel') . '</a>',
    '#weight' => 21,
  );
  $form = drupal_render($form_build);

  // Add the new form.
  if ($pid) {
    $commands[] = ajax_command_after('#comment-wrapper-' . $pid . '>.comment', $form);
  }
  else {
    // Display form above comments if ordered newest first
    $place = ajax_comments_node_type_active($node->type);

    if ($place == AJAX_COMMENTS_NEW_ON_TOP) {
      $commands[] = ajax_command_prepend('#comment-wrapper-nid-' . $node->nid, $form);
    } else {
      $commands[] = ajax_command_append('#comment-wrapper-nid-' . $node->nid, $form);
    }
  }

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

I think actually this would need fine grained control as you can't make the assumption that the site owner wishes the form to be at the top when displaying newest comments first. Would be easy to build on Georgique's work to add a setting for this though.

Another thought... what about comment preview display?

I'm happy to make a combined patch if that's alright with you Georgique?

Paul

Georgique’s picture

@drupaul.co.uk I haven't understood you completely about an option, I guess option is implemented in my patch.

g10’s picture

patch does not apply neither against latest dev
manually added the changes and made a new patch (against 7.x-1.0-alpha1+6-dev)

as for the suggestion of #1 : this is a logical addition, to add the comment form at the top (instead of the bottom), as it is all a bit detached ui-wise: post comment button at top, form at bottom of comments, new comment back at top :/

g10’s picture

+ patch made from #2

although it works, the logic seems flawed at first sight (2am here, so I might be missing something)
to clarify: you check if the ajax_comments module is activated for a given node type, and based on that you determine the place?

$place = ajax_comments_node_type_active($node->type);
paulhudson’s picture

Yes it's not that obvious, the function name is now misleading after my suggested addition.

ajax_comments_node_type_active() previously returned a boolean value which was set at /admin/config/content/ajax_comments to enable/disable ajax comments per node type.

However it now returns one of our three constants:

define('AJAX_COMMENTS_DISABLED', 0);
define('AJAX_COMMENTS_NEW_ON_TOP', 1);
define('AJAX_COMMENTS_NEW_ON_BOTTOM', 2);

I guess we should change the function name. :-)

andrew_mallis’s picture

Can someone please re-roll a single patch against dev?
This functionality is important. For the time being I'm just prepending the form for my use-case

-    $commands[] = ajax_command_append('#comment-wrapper', $form);
+    $commands[] = ajax_command_prepend('#comment-wrapper', $form);

since the patches don't apply, and/or are a little tangled.

andrew_mallis’s picture

(happy to test a a new patch)

Georgique’s picture

StatusFileSize
new9.51 KB

This patch was made against latest dev, please test it.
In this patch included:
* Subject opportunities;
* Small code improvements;
* Function ajax_comments_node_type_active() renamed to ajax_comments_new_comments_mode() and it's logic improved.
Haven't implemented changes for the form (not needed for me), so if somebody can participate, please do it.

andrew_mallis’s picture

There is nothing in the master branch.
Shouldn't the latest DEV be there?

I checked out 7.x-1.x and the patch applied cleanly.

In the options available at admin/config/content/ajax_comments
I selected "placed at the top", but the comments were still "placed at the bottom"
disabling ajax comments works as expected

I'm not sure where the issue is.
Variables table does not show any of the options that are saved from the form, only wether ajax comments are enabled or disabled.
Since our defaults are to set them to the bottom, that's where they end up.

suggested wording change:
placed at the top -> placed above existing comments
placed at the bottom -> placed below existing comments

atiba’s picture

just to let you know that Georgique's (#9) patch worked for me.

thanks a lot!

mstef’s picture

Doesn't work at all for me. I chose 'placed at the top' for each node type. Comments go to the bottom...

Berliner-dupe’s picture

#9 dont work by me ... all new comments are in bottom furthermore ....

Is another fix available?

muschpusch’s picture

Status: Needs review » Needs work

Ok this seems like a useful feature! Could you guys agree on if this is working or not? Setting it to "needs work" for the time being...

formatC&#039;vt’s picture

Issue summary: View changes
Status: Needs work » Postponed (maintainer needs more info)

Please, try Comment goodness module for this

formatC&#039;vt’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

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