You are here

function flag_flag in Flag 7.2

Same name and namespace in other branches
  1. 5 flag.module \flag_flag()
  2. 6.2 flag.module \flag_flag()
  3. 6 flag.module \flag_flag()

Implements hook_flag(). Trigger actions if any are available.

1 string reference to 'flag_flag'
flag_action_info_alter in includes/flag.actions.inc
Implements hook_action_info_alter().

File

./flag.module, line 1083
The Flag module.

Code

function flag_flag($action, $flag, $content_id, $account) {
  if (module_exists('trigger')) {
    $context['hook'] = 'flag';
    $context['account'] = $account;
    $context['flag'] = $flag;
    $context['op'] = $action;

    // We add to the $context all the objects we know about:
    $context = array_merge($flag
      ->get_relevant_action_objects($content_id), $context);

    // The primary object the actions work on.
    $object = $flag
      ->fetch_content($content_id);

    // Generic "all flags" actions.
    foreach (trigger_get_assigned_actions('flag_' . $action) as $aid => $action_info) {

      // The 'if ($aid)' is a safeguard against http://drupal.org/node/271460#comment-886564
      if ($aid) {
        actions_do($aid, $object, $context);
      }
    }

    // Actions specifically for this flag.
    foreach (trigger_get_assigned_actions('flag_' . $action . '_' . $flag->name) as $aid => $action_info) {
      if ($aid) {
        actions_do($aid, $object, $context);
      }
    }
  }
  if (module_exists('rules')) {
    $event_name = ($action == 'flag' ? 'flag_flagged_' : 'flag_unflagged_') . $flag->name;

    // We only support flags on entities.
    if (entity_get_info($flag->content_type)) {
      $variables = array(
        'flag' => $flag,
        'flagged_' . $flag->content_type => $content_id,
        'flagging_user' => $account,
      );
      rules_invoke_event_by_args($event_name, $variables);
    }
  }
}