You are here

public static function TokenTreeTable::preRenderTokenTree in Token 8

Pre-render the token tree to transform rows in the token tree.

Parameters

array $element:

Return value

array The processed element.

File

src/Element/TokenTreeTable.php, line 56

Class

TokenTreeTable
Provides a render element for a token tree table.

Namespace

Drupal\token\Element

Code

public static function preRenderTokenTree($element) {
  $multiple_token_types = count($element['#token_tree']) > 1;
  foreach ($element['#token_tree'] as $token_type => $type_info) {

    // Do not show nested tokens.
    if (!empty($type_info['nested']) && empty($element['#show_nested'])) {
      continue;
    }
    if ($multiple_token_types) {
      $row = static::formatRow($token_type, $type_info, $element['#columns'], TRUE);
      $element['#rows'][] = $row;
    }
    foreach ($type_info['tokens'] as $token => $token_info) {
      if (!empty($token_info['restricted']) && empty($element['#show_restricted'])) {
        continue;
      }
      if ($element['#skip_empty_values'] && empty($token_info['value']) && !empty($token_info['parent']) && !isset($tree[$token_info['parent']]['value'])) {
        continue;
      }
      if ($multiple_token_types && !isset($token_info['parent'])) {
        $token_info['parent'] = $token_type;
      }
      $row = static::formatRow($token, $token_info, $element['#columns']);
      $element['#rows'][] = $row;
    }
  }
  if (!empty($element['#rows'])) {
    $element['#attached']['library'][] = 'token/jquery.treeTable';
  }

  // Fill headers if one is not specified.
  if (empty($element['#header'])) {
    $column_map = [
      'name' => t('Name'),
      'token' => t('Token'),
      'value' => t('Value'),
      'description' => t('Description'),
    ];
    foreach ($element['#columns'] as $col) {
      $element['#header'][] = $column_map[$col];
    }
  }
  $element['#attributes']['class'][] = 'token-tree';
  if ($element['#click_insert']) {
    $element['#caption'] = t('Click a token to insert it into the field you\'ve last clicked.');
    $element['#attributes']['class'][] = 'token-click-insert';
  }
  return $element;
}