You are here

function template_preprocess_flag in Flag 7.2

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

A preprocess function for our theme('flag'). It generates the variables needed there.

The $variables array initially contains the following arguments:

  • $flag
  • $action
  • $content_id
  • $after_flagging

See 'flag.tpl.php' for their documentation.

File

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

Code

function template_preprocess_flag(&$variables) {
  global $user;
  $initialized =& drupal_static(__FUNCTION__, array());

  // Some typing shotcuts:
  $flag =& $variables['flag'];
  $action = $variables['action'];
  $content_id = $variables['content_id'];
  $flag_css_name = str_replace('_', '-', $flag->name);

  // Generate the link URL.
  $link_type = $flag
    ->get_link_type();
  $link = module_invoke($link_type['module'], 'flag_link', $flag, $action, $content_id);
  if (isset($link['title']) && empty($link['html'])) {
    $link['title'] = check_plain($link['title']);
  }

  // Replace the link with the access denied text if unable to flag.
  if ($action == 'unflag' && !$flag
    ->access($content_id, 'unflag')) {
    $link['title'] = $flag
      ->get_label('unflag_denied_text', $content_id);
    unset($link['href']);
  }

  // Anonymous users always need the JavaScript to maintain their flag state.
  if ($user->uid == 0) {
    $link_type['uses standard js'] = TRUE;
  }

  // Load the JavaScript/CSS, if the link type requires it.
  if (!isset($initialized[$link_type['name']])) {
    if ($link_type['uses standard css']) {
      drupal_add_css(drupal_get_path('module', 'flag') . '/theme/flag.css');
    }
    if ($link_type['uses standard js']) {
      drupal_add_js(drupal_get_path('module', 'flag') . '/theme/flag.js');
    }
    $initialized[$link_type['name']] = TRUE;
  }
  $variables['link'] = $link;
  $variables['link_href'] = isset($link['href']) ? check_url(url($link['href'], $link)) : FALSE;
  $variables['link_text'] = isset($link['title']) ? $link['title'] : $flag
    ->get_label($action . '_short', $content_id);
  $variables['link_title'] = isset($link['attributes']['title']) ? check_plain($link['attributes']['title']) : check_plain(strip_tags($flag
    ->get_label($action . '_long', $content_id)));
  $variables['status'] = $action == 'flag' ? 'unflagged' : 'flagged';
  $variables['flag_name_css'] = $flag_css_name;
  $variables['flag_wrapper_classes_array'] = array();
  $variables['flag_wrapper_classes_array'][] = 'flag-wrapper';
  $variables['flag_wrapper_classes_array'][] = 'flag-' . $flag_css_name;
  $variables['flag_wrapper_classes_array'][] = 'flag-' . $flag_css_name . '-' . $content_id;
  $variables['flag_wrapper_classes'] = implode(' ', $variables['flag_wrapper_classes_array']);
  $variables['flag_classes_array'] = array();
  $variables['flag_classes_array'][] = 'flag';
  $variables['flag_classes_array'][] = $variables['action'] . '-action';
  $variables['flag_classes_array'][] = 'flag-link-' . $flag->link_type;
  if (isset($link['attributes']['class'])) {
    $link['attributes']['class'] = is_string($link['attributes']['class']) ? array_filter(explode(' ', $link['attributes']['class'])) : $link['attributes']['class'];
    $variables['flag_classes_array'] = array_merge($variables['flag_classes_array'], $link['attributes']['class']);
  }
  if ($variables['after_flagging']) {
    $inverse_action = $action == 'flag' ? 'unflag' : 'flag';
    $variables['message_text'] = $flag
      ->get_label($inverse_action . '_message', $content_id);
    $variables['flag_classes_array'][] = $variables['status'];
  }
  $variables['flag_classes'] = implode(' ', $variables['flag_classes_array']);

  // Backward compatibility: The following section preserves some deprecated
  // variables either to make old flag.tpl.php files continue to work, or to
  // prevent PHP from generating E_NOTICEs there. @todo: Remove these sometime.
  $variables['setup'] = FALSE;
  $variables['last_action'] = $variables['status'];
}