2

I'm using the Achievements module to award the badges to users on certain actions.

In the following example user will get badges after posting 5 and 10 comments.

function MYMODULE_comment_insert($comment) {
  $current_count = achievements_storage_get('comment-count', $comment->uid) + 1;
  achievements_storage_set('comment-count', $current_count, $comment->uid);

  foreach (array(5,10) as $count) {
    if ($current_count == $count) {
      achievements_unlocked('comment-count-' . $count, $comment->uid);
    }
  }
}

The above code is working, user is able to achieve badge after posting 5 comments but since the comment form is being submitted through AJAX, the rewarded achievements notification popup is not being shown until page refresh.

So how can I show achievement notification popup for Ajax callbacks? Any suggestions?

3
  • Do you use drupal.org/project/ajax_comments or your own code for ajaxify comments? Commented Mar 16, 2016 at 11:17
  • @MariaKvitova No I'm using Flag and Drupal Wall modules which uses Ajax. Thanks Commented Mar 16, 2016 at 12:12
  • Could you tell what version of Drupal wall do you use? I tried 7.x-2.7 it doesn't seem to work :( You can find that message appears from hook_page_alter cgit.drupalcode.org/achievements/tree/… So you can use it in your ajax-callback and return message as ajax-command. Unfortunately I can't give you more details, because I couldn't force Drupal Wall to work. Commented Mar 17, 2016 at 10:49

2 Answers 2

1

Here is another try at this (various assumptions follow):

  • The whole thing should run through the Wall module's code, since this is the base for any other interaction happening.
  • By means of the Form API and AJAX callbacks, there is a possible entry point. This is to wrap the submit callback for the _drupal_wall_comment_post_form into another callback that will make sure the Achievements' related elements are updated.
  • The steps in this process would be:

    1. Use hook_form_alter to modify _drupal_wall_comment_post_form
    2. Modify _drupal_wall_comment_post_form by hijacking the ajax callback for the wall comment submissions: $form['drupal_wall_submit']['#ajax']['callback'] =>_your_custom_wall_ajax_callback

    3. Wrap the original callback in your ajax callback and extend it to trigger the Achievements popup functionality using the Ajax framework commands family:

      function _your_custom_wall_ajax_callback($form, $form_state) {  
        $wall_wrapper = $form['drupal_wall_submit']['#ajax']['wrapper'];
        $wall_content = _drupal_wall_comment_ajax_form_post($form, $form_state);
        $commands = array();
        $commands[] = ajax_command_replace($wall_wrapper, $wall_content);
      
        // This is where you can write ajax commands to trigger Achievements functionality
        $commands[] = ajax_command_*();
      
        return array('#type' => 'ajax', '#commands' => $commands);
      }
      

Hope this brings you a step further!

1
  • You are welcome! It would be great to update your question with things you tried and didn't work, what would you say? Cheers Commented Mar 21, 2016 at 21:53
-1

You could try using Rules to add a rule for when a user post a comment and then do your thing

After creating the rule you can set conditions and actions, so you can check for the comment count and then act accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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