You are here

class TokenTreeTable in Token 8

Provides a render element for a token tree table.

Plugin annotation

@RenderElement("token_tree_table");

Hierarchy

Expanded class hierarchy of TokenTreeTable

2 #type uses of TokenTreeTable
TokenDevelController::renderTokenTree in src/Controller/TokenDevelController.php
Render the token tree for the specified entity.
TreeBuilder::buildRenderable in src/TreeBuilder.php
Build a render array with token tree built as per specified options.

File

src/Element/TokenTreeTable.php, line 13

Namespace

Drupal\token\Element
View source
class TokenTreeTable extends Table {
  protected static $cssFilter = [
    ' ' => '-',
    '_' => '-',
    '/' => '-',
    '[' => '-',
    ']' => '',
    ':' => '--',
    '?' => '',
    '<' => '-',
    '>' => '-',
  ];

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#header' => [],
      '#rows' => [],
      '#token_tree' => [],
      '#columns' => [
        'name',
        'token',
        'description',
      ],
      '#empty' => '',
      '#show_restricted' => FALSE,
      '#show_nested' => FALSE,
      '#skip_empty_values' => FALSE,
      '#click_insert' => TRUE,
      '#sticky' => FALSE,
      '#responsive' => TRUE,
      '#input' => FALSE,
      '#pre_render' => [
        [
          $class,
          'preRenderTokenTree',
        ],
        [
          $class,
          'preRenderTable',
        ],
      ],
      '#theme' => 'table__token_tree',
      '#attached' => [
        'library' => [
          'token/token',
        ],
      ],
    ];
  }

  /**
   * Pre-render the token tree to transform rows in the token tree.
   *
   * @param array $element
   *
   * @return array
   *   The processed element.
   */
  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;
  }
  protected static function cleanCssIdentifier($id) {
    return 'token-' . Html::cleanCssIdentifier(trim($id, '[]'), static::$cssFilter);
  }
  protected static function formatRow($token, $token_info, $columns, $is_group = FALSE) {
    $row = [
      'id' => static::cleanCssIdentifier($token),
      'data-tt-id' => static::cleanCssIdentifier($token),
      'class' => [],
      'data' => [],
    ];
    foreach ($columns as $col) {
      switch ($col) {
        case 'name':
          $row['data'][$col] = $token_info['name'];
          break;
        case 'token':
          $row['data'][$col]['data'] = $token;
          $row['data'][$col]['class'][] = 'token-key';
          break;
        case 'description':
          $row['data'][$col] = isset($token_info['description']) ? $token_info['description'] : '';
          break;
        case 'value':
          $row['data'][$col] = !$is_group && isset($token_info['value']) ? $token_info['value'] : '';
          break;
      }
    }
    if ($is_group) {

      // This is a token type/group.
      $row['class'][] = 'token-group';
    }
    elseif (!empty($token_info['parent'])) {
      $row['data-tt-parent-id'] = static::cleanCssIdentifier($token_info['parent']);
      unset($row['parent']);
    }
    return $row;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Table::preRenderTable public static function #pre_render callback to transform children of an element of #type 'table'.
Table::processTable public static function #process callback for #type 'table' to add tableselect support.
Table::validateTable public static function #element_validate callback for #type 'table'.
Table::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElement::valueCallback 1
TokenTreeTable::$cssFilter protected static property
TokenTreeTable::cleanCssIdentifier protected static function
TokenTreeTable::formatRow protected static function
TokenTreeTable::getInfo public function Returns the element properties for this element. Overrides Table::getInfo
TokenTreeTable::preRenderTokenTree public static function Pre-render the token tree to transform rows in the token tree.