You are here

function flag_flag::theme in Flag 7.3

Same name and namespace in other branches
  1. 5 flag.inc \flag_flag::theme()
  2. 6.2 flag.inc \flag_flag::theme()
  3. 6 flag.inc \flag_flag::theme()
  4. 7.2 flag.inc \flag_flag::theme()

Renders a flag/unflag link.

This is a wrapper around theme('flag') that channels the call to the right template file.

Parameters

string $action: The action the link is about to carry out, either "flag" or "unflag".

int $entity_id: The ID of the object to flag.

array $variables: An array of further variables to pass to theme('flag'). For the full list of parameters, see flag.tpl.php. Of particular interest:

  • after_flagging: Set to TRUE if this flag link is being displayed as the result of a flagging action.
  • errors: An array of error messages.

Return value

string The HTML for the flag link.

File

includes/flag/flag_flag.inc, line 1551
Contains the flag_flag class. Flag type classes use an object oriented style inspired by that of Views 2.

Class

flag_flag
This abstract class represents a flag, or, in Views 2 terminology, "a handler".

Code

function theme($action, $entity_id, $variables = array()) {
  static $js_added = array();
  global $user;
  $after_flagging = !empty($variables['after_flagging']);

  // If the flagging user is anonymous, set a boolean for the benefit of
  // JavaScript code. Currently, only our "anti-crawlers" mechanism uses it.
  if ($user->uid == 0 && !isset($js_added['anonymous'])) {
    $js_added['anonymous'] = TRUE;
    drupal_add_js(array(
      'flag' => array(
        'anonymous' => TRUE,
      ),
    ), 'setting');
  }

  // If the flagging user is anonymous and the page cache is enabled, we
  // update the links through JavaScript.
  if ($this
    ->uses_anonymous_cookies() && !$after_flagging) {
    if ($this->global) {

      // In case of global flags, the JavaScript template is to contain
      // the opposite of the current state.
      $js_action = $action == 'flag' ? 'unflag' : 'flag';
    }
    else {

      // In case of non-global flags, we always show the "flag!" link,
      // and then replace it with the "unflag!" link through JavaScript.
      $js_action = 'unflag';
      $action = 'flag';
    }
    if (!isset($js_added[$this->name . '_' . $entity_id])) {
      $js_added[$this->name . '_' . $entity_id] = TRUE;
      $js_template = theme($this
        ->theme_suggestions(), array(
        'flag' => $this,
        'action' => $js_action,
        'entity_id' => $entity_id,
        'after_flagging' => $after_flagging,
      ));
      drupal_add_js(array(
        'flag' => array(
          'templates' => array(
            $this->name . '_' . $entity_id => $js_template,
          ),
        ),
      ), 'setting');
    }
  }
  return theme($this
    ->theme_suggestions(), array(
    'flag' => $this,
    'action' => $action,
    'entity_id' => $entity_id,
  ) + $variables);
}