Thanks for this module, it is a great addition to the flag module and a crucial part when building a comparison list of nodes (products).

When using javascript toggle the limit reached message appears on next page load. This means that the message is presented out of context of a user flagging something. I would suggest making the message appear through ajax straight after the limit has been reached in stead of after the next page load. This would improve the message function and prevent confusion.

I would also suggest making the the message and type optional, e.g Message: yes or no and type: status , warning or error. Or maybe at least changing it to warning. The current status 'error' seems a bit severe in most cases.

Comments

steveoriol’s picture

This is true, it would be really good ...

oresh’s picture

Ok, so i had the same issue and tried to show the text on ajax request.
So what the module does practically — it shows user the error and unflags the thing he just flagged :)
$unflag = flag('unflag', $flag->name, $entity_id, $account, TRUE);

The flag() function doesn't have any way to pass a message or something to it. So what happens, is that the flag is first added than removed (unflagged) and we see the result of the flag_build_javascript_info function.

I tried die() or return or print and other stuff, but failed :( So currently no idea, how to implement that.
The only thing I guess can be done here — is adding a script that checks flags being clicked and on failing to flag show the error to the user. The error though can be added in the settings page and stored in drupal js settings.

Or is there a solution for this?

tko’s picture

Been banging my head on this all day and can't find a way to add a custom message when limits are reached via ajax flags.

The event trigger flagGlobalBeforeLinkUpdate provides data.newLink with the generated error message inserted. You could inject the new error in there via JS but at that point the script doesn't know that the limit was reached. Since flag_limiter is only unflagging a flagged flag if the limit is exceeded there is no way to pass the new error into JS from hook_flag_flag.

EDIT:

I've just had a look at flag.api.php and there is a hook_flag_validate() which by its own definitions states "Perform custom validation on a flag before flagging/unflagging."

I don't understand why flag_limiter is not using this but instead flagging then unflagging with hook_flag_flag. Am I missing something?

hook_flag_validate is self explanatory and works with AJAX for error messages:

function hook_flag_validate($action, $flag, $entity_id, $account, $skip_permission_check, $flagging) {
  // We're only operating on the "test" flag, and users may always unflag.
  if ($flag->name == 'test' && $action == 'flag') {
    // Get all flags set by the current user.
    $flags = flag_get_user_flags('node', NULL, $account->uid, $sid = NULL, $reset = FALSE);
    // Check if this user has any flags of this type set.
    if (isset($flags['test'])) {
      $count = count($flags[$flag->name]);
      if ($count >= 2) {
        // Users may flag only 2 nodes with this flag.
        return(array('access-denied' => t('You may only flag 2 nodes with the test flag.')));
      }
    }
  }
}