I am getting this error Exception: Flag action not allowed for given flagging entity properties. in flagging_save() when my hook_flag_validate() returns an error. Isn't that why we have this hook?
I would think it would show the contents of $flag->errors rather than throwing up its hands and quitting.
function mymodule_flag_validate($action, $flag, $entity_id, $account, $skip_permission_check, $flagging) {
global $user;
// Let's see if they are trying to register?
if ($action == 'flag' && $flag->name == 'registration') {
if ($user->uid == 3 || $user->uid == 38) {
// Do my debugging here.
}
// Are they already registered?
if ($flag->is_flagged($entity_id, $account->uid)) {
$instance = node_load($entity_id);
$event = node_load($instance->field_related_events[LANGUAGE_NONE][0]['target_id']);
return array('already-registered' => t(variable_get('captconnect_custom_duplicate_registration',
'You were already registered for "@title."'),
array('@title' => $event->title)));
}
// Is the event instance at capacity yet?
$instance = entity_metadata_wrapper('node', node_load($entity_id));
$capacity = $instance->field_max_registration->value();
if (empty($capacity)) {
$capacity = 0;
}
// How many registrtations do we have currently?
$num_regs = $flag->get_count($entity_id);
if ($num_regs >= $capacity && $capacity > 0) {
return array('event-full' => t(variable_get('captconnect_custom_event_full',
'We are sorry, but this event is full. You may try registering for another time or date.')));
}
}
}
Comments
Comment #2
nancydruI tried changing
flagging_save():This is sort of okay, except that the flag is set when it should not be on an error, and in this code, the message is displayed twice when there is only one error.
Comment #3
nancydruComment #4
nancydruI tried to unflag this when there was an error, but it is causing a 500 return on my production system, and working correctly on my test system.
Comment #5
joachim commentedYou're right that the contents of $flag->errors should be added to the exception message to give a reason. However, flagging_save() shouldn't call drupal_set_message(), as it might not be being used in a context where that's useful or desirable.
Comment #6
nancydruWhen we throw an exception, it is really ugly and the user has no idea what to do. Even if the error message was added, they'd be lost.
Comment #7
joachim commentedIt's up to the code calling flagging_save() to catch that and handle it appropriately.
Comment #8
nancydruWell, that would be the flag module itself. The user is just clicking a normal flag link.
Comment #9
joachim commentedI don't think that's the case -- nothing calls flagging_save() in Flag apart from test code. It's there as an external API.
Comment #10
nancydruThis is the only reference I can find.
Comment #11
joachim commentedYup, we declare it as a callback, but that's for Entity API, and we don't make use of it ourselves.
Comment #12
nancydruA trace shows it being called from line 286 in entity.module. So you're saying that Entity is at fault here?
Comment #13
joachim commentedNot necessarily entity, but whoever is calling the entity save on the flagging.
http://www.drupalcontrib.org/api/drupal/contributions!entity!entity.modu... says:
> In case of failures, an exception is thrown.
So whoever calls entity_save() should be catching exceptions.
Comment #14
nancydruIt seems to be, but leaves the user out in la-la land with no idea what to do.
Comment #15
joachim commentedYup, as I say, I'm happy to take a patch to put a better message in that exception.
Catching it (and then not failing to dramatically, and giving the user a clue!) is the job of the code that's calling entity_save().
Comment #16
nancydruOkay, here's my code as a patch.
I'll open an issue on Entity API.
Comment #18
nancydruI don't know how to write tests.
Comment #19
joachim commentedunshift adds something to an array. It's array_shift() you need here.
> I'll open an issue on Entity API.
I don't think it's Entity API that's the problem. Something else is calling entity_save().
Comment #20
nancydruArgh! That's what I get for trying to recreate the code instead of copying it.
Comment #21
ivnishDrupal 7 is EOL. Issue will be closed, but patches are still here