You are here

function flag_contextual_links_view_alter in Flag 7.3

Same name and namespace in other branches
  1. 7.2 flag.module \flag_contextual_links_view_alter()

File

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

Code

function flag_contextual_links_view_alter(&$element, $items) {
  if (isset($element['#element']['#entity_type'])) {
    $entity_type = $element['#element']['#entity_type'];

    // Get the entity out of the element. This requires a bit of legwork.
    if (isset($element['#element']['#entity'])) {

      // EntityAPI entities will all have the entity in the same place.
      $entity = $element['#element']['#entity'];
    }
    elseif (isset($element['#element']['#' . $entity_type])) {

      // Node module at least puts it here.
      $entity = $element['#element']['#' . $entity_type];
    }
    else {

      // Give up.
      return;
    }

    // Get all possible flags for this entity type.
    $flags = flag_get_flags($entity_type);
    foreach ($flags as $name => $flag) {
      if (!$flag->show_contextual_link) {
        continue;
      }
      list($entity_id) = entity_extract_ids($entity_type, $entity);
      if (!$flag
        ->access($entity_id) && (!$flag
        ->is_flagged($entity_id) || !$flag
        ->access($entity_id, 'flag'))) {

        // User has no permission to use this flag or flag does not apply to
        // this object. The link is not skipped if the user has "flag" access
        // but not "unflag" access (this way the unflag denied message is
        // shown).
        continue;
      }
      $element['#links']['flag-' . $name] = array(
        'title' => $flag
          ->theme($flag
          ->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id),
        'html' => TRUE,
      );
    }
  }
}