1 system.pages.inc system_token_browser_tree_build_content($types, $global_types, $show_restricted)

Helper function to generate a render array of token types.

@since 1.30.0 Function added.

Parameters

array|string $types: An array of token types to display in the tree or the string 'all'.

bool $global_types: Whether or not to include global types in the tree.

Return value

array: The constructed array of types keyed by type.

File

core/modules/system/system.pages.inc, line 115
System module non-administrative page callbacks.

Code

function system_token_browser_tree_build_content($types, $global_types, $show_restricted) {
  global $language;

  $info = token_get_info();
  $rows = array();
  $posinset = 1;

  if ($types === 'all') {
    $types = array_keys($info['types']);
  }
  elseif ($global_types) {
    $types = array_merge($types, token_get_global_token_types());
  }

  $hash = hash('sha256', serialize(array(
    'token_types' => $types,
    'global_types' => $global_types,
    'language' => $language->langcode,
    'show_restricted' => $show_restricted,
  )));

  $cid = 'token-browser-build:' . $hash;

  if ($cache = cache_get($cid, 'cache_token')) {
    return $cache->data;
  }

  foreach ($info['types'] as $type => $type_info) {
    if (!in_array($type, $types) || substr($type, 0, 4) === 'list') {
      continue;
    }

    $rows[] = array(
      'data' => array(
        array(
          'data' => '<button aria-label="Expand">Expand</button>' . $type_info['name'],
          'data-type' => $type,
          'role' => 'gridcell',
          'class' => array('token-name'),
        ),
        array(
          'data' => $type,
          'role' => 'gridcell',
          'class' => array('token-raw'),
        ),
        array(
          'data' => isset($type_info['description']) ? $type_info['description'] : '',
          'role' => 'gridcell',
          'class' => array('token-description'),
        ),
      ),
      'role' => 'row',
      'aria-level' => '1',
      'aria-posinset' => $posinset++,
      'aria-expanded' => 'false',
      'aria-busy' => 'false',
      'class' => array('tree-grid-parent'),
      'no_striping' => TRUE,
    );
  }

  $build = array(
    '#theme' => 'table',
    '#header' => array(
      t('Name'),
      t('Token'),
      t('Description'),
    ),
    '#rows' => $rows,
    '#attributes' => array(
      'role' => 'treetable',
      'aria-label' => 'Token Browser',
      'class' => array(
        'tree-grid',
      ),
    ),
    '#attached' => array(
      'library' => array(
        array('system', 'token'),
      ),
    ),
    '#cache' => array(
      'cid' => 'token-browser-render:' . $hash,
      'bin' => 'cache_token',
    ),
  );

  cache_set($cid, $build, 'cache_token');

  return $build;
}