You are here

function flag_entity_view in Flag 7.3

Same name and namespace in other branches
  1. 8.4 flag.module \flag_entity_view()
  2. 7.2 flag.module \flag_entity_view()

Implements hook_entity_view().

Handles the 'show_in_links' and 'show_as_field' flag options.

Note this is broken for taxonomy terms for version of Drupal core < 7.17.

File

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

Code

function flag_entity_view($entity, $type, $view_mode, $langcode) {

  // Get all possible flags for this entity type.
  $flags = flag_get_flags($type);
  foreach ($flags as $flag) {

    // Check if the flag outputs on entity view.
    if (!($flag->show_as_field || $flag
      ->shows_in_entity_links($view_mode))) {

      // Flag is not configured to output on entity view, so skip it to save on
      // calls to access checks.
      continue;
    }
    $entity_id = $flag
      ->get_entity_id($entity);

    // For a new, unsaved entity, make a dummy entity ID so that the flag
    // handler can remember the entity. This allows access to the flag to be
    // correctly handled in node and comment preview.
    if (is_null($entity_id)) {
      $entity_id = 'new';
    }
    $flag
      ->remember_entity($entity_id, $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
      // entity. The link is not skipped if the user has "flag" access but
      // not "unflag" access (this way the unflag denied message is shown).
      continue;
    }

    // We're good to go. Output the flag in the appropriate manner(s).
    // The old-style entity links output.
    if ($flag
      ->shows_in_entity_links($view_mode)) {

      // 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($entity_id) ? 'unflag' : 'flag', $entity_id),
        'html' => TRUE,
      );
    }

    // The pseudofield output.
    if ($flag->show_as_field) {
      $entity->content['flag_' . $flag->name] = array(
        '#markup' => $flag
          ->theme($flag
          ->is_flagged($entity_id) ? 'unflag' : 'flag', $entity_id, array(
          'needs_wrapping_element' => TRUE,
        )),
      );
    }
  }

  // If any links were made, add them to the entity's links array.
  if (isset($links)) {
    $entity->content['links']['flag'] = array(
      '#theme' => 'links',
      '#links' => $links,
      '#attributes' => array(
        'class' => array(
          'links',
          'inline',
        ),
      ),
    );
  }
}