You are here

public function MatcherTokensTrait::insertTokenList in Linkit 8.4

Same name and namespace in other branches
  1. 8.5 src/MatcherTokensTrait.php \Drupal\linkit\MatcherTokensTrait::insertTokenList()

Inserts a form element with a list of available tokens.

Parameters

$form: The form array to append the token list to.

array $types: An array of token types to use.

2 calls to MatcherTokensTrait::insertTokenList()
EntityMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/EntityMatcher.php
Form constructor.
TermMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/TermMatcher.php
Form constructor.

File

src/MatcherTokensTrait.php, line 23
Contains \Drupal\linkit\MatcherTokensTrait.

Class

MatcherTokensTrait
Provides friendly methods for matchers using tokens.

Namespace

Drupal\linkit

Code

public function insertTokenList(&$form, array $types = array()) {
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {

    // Add the token tree UI.
    $form['token_tree'] = array(
      '#theme' => 'token_tree_link',
      '#token_types' => $types,
      '#dialog' => TRUE,
      '#weight' => -90,
    );
  }
  else {
    $token_items = array();
    foreach ($this
      ->getAvailableTokens($types) as $type => $tokens) {
      foreach ($tokens as $name => $info) {
        $token_description = !empty($info['description']) ? $info['description'] : '';
        $token_items[$type . ':' . $name] = "[{$type}:{$name}]" . ' - ' . $info['name'] . ': ' . $token_description;
      }
    }
    if (count($token_items)) {
      $form['tokens'] = array(
        '#type' => 'details',
        '#title' => t('Available tokens'),
        '#weight' => -90,
      );
      $form['tokens']['list'] = array(
        '#theme' => 'item_list',
        '#items' => $token_items,
      );
    }
  }
}