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.')));
    }
  }
}
CommentFileSizeAuthor
#20 2648446-20.patch695 bytesnancydru
#16 2648446-16.patch697 bytesnancydru

Comments

NancyDru created an issue. See original summary.

nancydru’s picture

I tried changing flagging_save():

  $result = $flag->flag('flag', $flagging->entity_id, $account, FALSE, $flagging);

  if (!$result) {
    if (empty($flag->errors)) {
      throw new Exception('Flag action not allowed for given flagging entity properties.');
    }
    else {
      drupal_set_message(t('The "@flag" action failed for the following reason(s).',
        array('@flag' => $flag->flag_short)), 'error');
      foreach ($flag->errors as $type => $message) {
        $error = '<span class="' . drupal_html_class($type) . '">'
          . $message . '</span>';
        drupal_set_message($error, 'error', FALSE);
      }
    }
  }

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.

nancydru’s picture

Priority: Normal » Major
nancydru’s picture

I 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.

  $result = $flag->flag('flag', $flagging->entity_id, $account, FALSE, $flagging);

  if (!$result) {
    if (empty($flag->errors)) {
      throw new Exception('Flag action not allowed for given flagging entity properties.');
    }
    else {
      drupal_set_message(t('The "@flag" action failed for the following reason(s).',
        array('@flag' => $flag->flag_short)), 'error');
      foreach ($flag->errors as $type => $message) {
        drupal_set_message($message, 'error', FALSE);

        // Let's unflag them.
        $result = $flag->unflag('unflag', $flagging->entity_id, $account, FALSE, $flagging);
      }
    }
  }
joachim’s picture

You'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.

nancydru’s picture

When 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.

joachim’s picture

It's up to the code calling flagging_save() to catch that and handle it appropriately.

nancydru’s picture

Well, that would be the flag module itself. The user is just clicking a normal flag link.

joachim’s picture

I don't think that's the case -- nothing calls flagging_save() in Flag apart from test code. It's there as an external API.

nancydru’s picture

function flag_entity_info() {
  $return = array(
    'flagging' => array(
      'label' => t('Flagging'),
      'controller class' => 'FlaggingController',
      'base table' => 'flagging',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'flagging_id',
        'bundle' => 'flag_name',
      ),
      // The following tells Field UI how to extract the bundle name from a
      // $flag object when we're visiting ?q=admin/.../manage/%flag/fields.
      'bundle keys' => array(
        'bundle' => 'name',
      ),
      'bundles' => array(),
      // The following tells EntityAPI how to save flaggings, thus allowing use
      // of Entity metadata wrappers (if present).
      'save callback' => 'flagging_save',
      'creation callback' => 'flagging_create',
    ),
  );

This is the only reference I can find.

joachim’s picture

Yup, we declare it as a callback, but that's for Entity API, and we don't make use of it ourselves.

nancydru’s picture

A trace shows it being called from line 286 in entity.module. So you're saying that Entity is at fault here?

joachim’s picture

Not 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.

nancydru’s picture

It seems to be, but leaves the user out in la-la land with no idea what to do.

  $result = $flag->flag('flag', $flagging->entity_id, $account, FALSE, $flagging);

  if (!$result) {
    if (empty($flag->errors)) {
      throw new Exception('Flag action not allowed for given flagging entity properties.');
    }
    else {
      $msg = array_unshift($flag->errors);
      throw new Exception($msg);
    }
  }
joachim’s picture

Yup, 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().

nancydru’s picture

Assigned: Unassigned » nancydru
Status: Active » Needs review
StatusFileSize
new697 bytes

Okay, here's my code as a patch.

I'll open an issue on Entity API.

Status: Needs review » Needs work

The last submitted patch, 16: 2648446-16.patch, failed testing.

nancydru’s picture

I don't know how to write tests.

joachim’s picture

+++ b/flag.module
@@ -148,7 +148,15 @@ function flagging_save($flagging) {
+      $msg = array_unshift($flag->errors);

unshift 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().

nancydru’s picture

Status: Needs work » Needs review
StatusFileSize
new695 bytes

Argh! That's what I get for trying to recreate the code instead of copying it.

ivnish’s picture

Assigned: nancydru » Unassigned
Status: Needs review » Closed (outdated)

Drupal 7 is EOL. Issue will be closed, but patches are still here