You are here

function flag_link in Flag 6.2

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

Implementation of hook_link().

File

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

Code

function flag_link($type, $object = NULL, $teaser = FALSE) {
  if (!isset($object) || !flag_fetch_definition($type)) {
    return;
  }
  global $user;

  // Get all possible flags for this content-type.
  $flags = flag_get_flags($type);
  foreach ($flags as $flag) {
    $content_id = $flag
      ->get_content_id($object);
    if (!$flag
      ->uses_hook_link($teaser)) {

      // Flag is not configured to show its link here.
      continue;
    }
    if (!$flag
      ->access($content_id) && (!$flag
      ->is_flagged($content_id) || !$flag
      ->access($content_id, 'flag'))) {

      // User has no permission to use this flag or flag does not apply to this
      // content. The link is not skipped if the user has "flag" access but
      // not "unflag" access (this way the unflag denied message is shown).
      continue;
    }

    // The flag links are actually fully rendered theme functions.
    // The HTML attribute is set to TRUE to allow whatever the themer desires.
    $links['flag-' . $flag->name] = array(
      'title' => $flag
        ->theme($flag
        ->is_flagged($content_id) ? 'unflag' : 'flag', $content_id),
      'html' => TRUE,
    );
  }
  if (isset($links)) {
    return $links;
  }
}