I am using AJAX Comments to post and edit comments on the same page. However, when I try to delete a comment with the Comment Goodness module, I am being redirected to a new page.

Therefore, I would like to be able to either bypass the confirmation you get when you try to delete a comment, and delete it right away, on the same page, or be able to get the confirmation form with AJAX.

I think the first one is much easier and only requires some deletion of code in the module. However, I haven't managed to do it yet, I'm trying though, but it would be nice if someone could help me with it.

Comments

formatC'vt’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

This is a bug on ajax_comments module side, a this bug is fixed in latest dev

Ayran’s picture

Hi, I have latest ajax_comments dev and latest comment goodness dev, but AJAX is not working when user wants to delete own comment - goes to new page with confirmation.

chrislabeard’s picture

I also have the latest of both ajax comments and comments goodness installed, and the delete link takes me to another page instead of firing via ajax.

Any updates on this?

formatC'vt’s picture

Project: Comment goodness » AJAX Comments
Version: 7.x-1.4 » 7.x-1.x-dev
Assigned: Unassigned » formatC'vt
Status: Closed (won't fix) » Active

I'll try to reproduce this bug on this week

formatC'vt’s picture

Project: AJAX Comments » Comment goodness
Assigned: formatC'vt » Unassigned
Status: Active » Needs work

I think this bug should be fixed on Comment goodness module side. By altering existing menu element:

  $items['comment/%/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'comment_confirm_delete_page',
    'page arguments' => array(1),
    'access arguments' => array('administer comments'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'comment.admin.inc',
    'weight' => 2,
  );

not by adding new one

$items['comment/%comment/delete-own'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_goodness_confirm_delete', 1),
    'access callback' => 'comment_goodness_delete_comment_access',
    'access arguments' => array(1),
    'type' => MENU_CALLBACK,
    'weight' => 2,
  );
msypes’s picture

I'm also trying to get this sort of behavior. I checked out Comment Goodness, but also didn't like that it uses a different location, as mentioned in #5. I've put together simple hook_menu_alter that works as far as access is continued, but whether or not that custom module is activated, I'm getting strange behavior. Maybe someone can guide in how Ajax Comments might be affecting the process flow, if at all.

I have a view of nodes that includes their comments with delete links. Clicking delete brings me to the delete confirm page, as expected, although I, too, would love for this to just bring up a confirm dialog. (Maybe I just need to figure out how to AJAXify that form?)

What I haven't been able to do is get a redirect back to the view page after comment deletion, either with code or Rules. In fact even deleting from a regular content page doesn't take me back there either. Is there something in this module that could do that, or should I be focusing on my own crappy code?

formatC'vt’s picture

can you provide that simple hook_menu_alter code you made?
AJAX Comments AJAXify links(forms) in ajax_comments_comment_view

msypes’s picture

Here you go:

function comment_owner_menu_alter(&$items) {
    $items['comment/%/delete']['access callback'] = 'comment_owner_deleteCommentAccess';
    $items['comment/%/delete']['access arguments'][] = 1;
}

function comment_owner_deleteCommentAccess($std_access_permission, $comment_id){
    if(is_numeric($comment_id)){
        $comment= comment_load($comment_id);

        if($comment){
            if(($GLOBALS['user']->uid != 0 && $comment->uid == $GLOBALS['user']->uid) || $GLOBALS['user']->uid == 1){
                return true;
            }
        }
    }
    return user_access($std_access_permission);
}

The above only solved part of the problem, as Views' built-in comment delete link handler has a hard-coded access method. So, I made a custom one in the same module, by duplicating, renaming, and modifying the original:

/**
 * Implementation of hook_views_data_alter
 *
 * This creates a field item to add via the Views UI.
 * To prevent a Views error about non-existent table columns,
 * relate the item to a real one in the __constructor()
 * of the extension class of views_handler_field.
 */
function comment_owner_views_data_alter(&$data) {
    $data['comment']['comment_owner_delete_link'] = array(
            'title' => t('Custom Comment Delete'),
            'help' => t('Custom delete comment link from Comment Owner module'),
            'field' => array(
                    'handler' => 'comment_owner_delete_link_handler'
            )
    );
}


/** Field handler to present a link to delete a comment.
 * This is meant to replace the stock handler provided by Views,
 * i.e., views_handler_field_comment_link_delete,
 * so as to use this module's access function.
 *
 * @ingroup views_field_handlers
 */
class comment_owner_delete_link_handler extends views_handler_field_comment_link {

    function access() {
        // Replaced with check internal to render_link function
        return true;
    }

    function render_link($data, $values) {
        $cid =  $this->get_value($values, 'cid');
        if (comment_owner_deleteCommentAccess('administer comments', $cid)) {
            $text = !empty($this->options['text']) ? $this->options['text'] : t('delete');
            $this->options['alter']['make_link'] = TRUE;
            $this->options['alter']['path'] = "comment/" . $cid . "/delete";
            $this->options['alter']['query'] = drupal_get_destination();

            return $text;
        }
        else {
            return false;
        }
    }
}

I also completed my solution to all these problems by including some Javascript to override the usual call of these links (formatted as php here merely for convenience):

Drupal.behaviors.AJAXyCommentDelete = {
        attach: function (context, settings) {
            // AJAX return behavior to ajax-content-receiver
            $('a.comment-delete, .comment-delete a').click(function(){
                $.get($(this).attr('href'), function(response){
                    var form = $(response).find('form#comment-confirm-delete');
                    $('#ajax-content-receiver').html(form).show();
                });
                return false;
            });
            $('#edit-cancel').attr('onClick',null).click(function(){
                $('#ajax-content-receiver').hide();
            });
            $('#ajax-content-receiver').on('click', '#edit-submit', function() {
                $('#ajax-content-receiver').hide();
            });
        }
    };

Finally, I have a couple of Rules to redirect back to the appropriate page when a comment is deleted, and hook_form_alter for the Cancel link:

function comment_owner_form_comment_confirm_delete_alter(&$form, &$form_state, $form_id){
    $form['description']['#markup'] = t('This action cannot be undone.');
    if (isset($_SERVER['HTTP_REFERER'])) {
        $form['actions']['cancel']['#href'] = $_SERVER['HTTP_REFERER'];
    }
}

I have an empty DIV in my page.tpl.php, to hold the incoming delete confirmation form, or whatever I may need in the future.

The end result of all these bits and pieces is that authenticated comment authors, as well as traditional admin roles, can delete comments. The links bring up a little modal box. Clicking "Cancel" makes it go away. Clicking the "Delete" button reloads the page with the comment gone. (The only thing that would make this slicker, IMO, would be to avoid the page refresh, but I've got other fish to fry at the moment.)

I also suggest moving this back to the AJAX Comments Issue Queue. I agree that Comment Goodness' solution is improper, but, as this is a feature request that could be rolled entirely into AJAX Comments, and Comment Goodness could be dispensed with entirely, it strikes me as a more logical placement.

formatC'vt’s picture

Assigned: Unassigned » formatC'vt
Status: Needs work » Needs review
StatusFileSize
new3.3 KB

Implemented altering existing menu element.

socialnicheguru’s picture

Status: Needs review » Reviewed & tested by the community

this worked perfectly.

mudasirweb’s picture

#9, Finally worked. Thanks @formatC'vt