You are here

function theme_token_tree in Token 7

Same name and namespace in other branches
  1. 6 token.pages.inc \theme_token_tree()

Provide a 'tree' display of nested tokens.

5 theme calls to theme_token_tree()
token_form_field_ui_field_edit_form_alter in ./token.module
Implements hook_form_FORM_ID_alter().
token_form_system_actions_configure_alter in ./token.module
Implements hook_form_FORM_ID_alter().
token_form_user_admin_settings_alter in ./token.module
Implements hook_form_FORM_ID_alter().
token_help in ./token.module
Implements hook_help().
token_page_output_tree in ./token.pages.inc
Page callback to output a token tree as an empty page.

File

./token.pages.inc, line 96
User page callbacks for the token module.

Code

function theme_token_tree($variables) {
  if (!empty($variables['dialog'])) {
    return theme_token_tree_link($variables);
  }
  $token_types = $variables['token_types'];
  $info = token_get_info();
  if ($token_types == 'all') {
    $token_types = array_keys($info['types']);
  }
  elseif ($variables['global_types']) {
    $token_types = array_merge($token_types, token_get_global_token_types());
  }
  $element = array(
    '#cache' => array(
      'cid' => 'tree-rendered:' . hash('sha256', serialize(array(
        'token_types' => $token_types,
        'global_types' => NULL,
      ) + $variables)),
      'bin' => 'cache_token',
    ),
  );
  if ($cached_output = token_render_cache_get($element)) {
    return $cached_output;
  }
  $options = array(
    'flat' => TRUE,
    'restricted' => $variables['show_restricted'],
    'depth' => $variables['recursion_limit'],
  );
  $multiple_token_types = count($token_types) > 1;
  $rows = array();
  foreach ($info['types'] as $type => $type_info) {
    if (!in_array($type, $token_types)) {
      continue;
    }
    if ($multiple_token_types) {
      $row = _token_token_tree_format_row($type, $type_info, TRUE);
      unset($row['data']['value']);
      $rows[] = $row;
    }
    $tree = token_build_tree($type, $options);
    foreach ($tree as $token => $token_info) {
      if (!empty($token_info['restricted']) && empty($variables['show_restricted'])) {
        continue;
      }
      if ($multiple_token_types && !isset($token_info['parent'])) {
        $token_info['parent'] = $type;
      }
      $row = _token_token_tree_format_row($token, $token_info);
      unset($row['data']['value']);
      $rows[] = $row;
    }
  }
  $element += array(
    '#theme' => 'tree_table',
    '#header' => array(
      t('Name'),
      t('Token'),
      t('Description'),
    ),
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'token-tree',
      ),
    ),
    '#empty' => t('No tokens available'),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'token') . '/token.js',
      ),
      'css' => array(
        drupal_get_path('module', 'token') . '/token.css',
      ),
      'library' => array(
        array(
          'token',
          'treeTable',
        ),
      ),
    ),
  );
  if ($variables['click_insert']) {
    $element['#caption'] = t('Click a token to insert it into the field you\'ve last clicked.');
    $element['#attributes']['class'][] = 'token-click-insert';
  }
  $output = drupal_render($element);
  token_render_cache_set($output, $element);
  return $output;
}