5 * Contains \Drupal\linkit\MatcherTokensTrait.
8 namespace Drupal\linkit;
11 * Provides friendly methods for matchers using tokens.
13 trait MatcherTokensTrait {
16 * Inserts a form element with a list of available tokens.
19 * The form array to append the token list to.
21 * An array of token types to use.
23 public function insertTokenList(&$form, array $types = array()) {
24 if (\Drupal::moduleHandler()->moduleExists('token')) {
25 // Add the token tree UI.
26 $form['token_tree'] = array(
27 '#theme' => 'token_tree_link',
28 '#token_types' => $types,
34 $token_items = array();
35 foreach ($this->getAvailableTokens($types) as $type => $tokens) {
36 foreach ($tokens as $name => $info) {
37 $token_description = !empty($info['description']) ? $info['description'] : '';
38 $token_items[$type . ':' . $name] = "[$type:$name]" . ' - ' . $info['name'] . ': ' . $token_description;
42 if (count($token_items)) {
43 $form['tokens'] = array(
45 '#title' => t('Available tokens'),
49 $form['tokens']['list'] = array(
50 '#theme' => 'item_list',
51 '#items' => $token_items,
58 * Gets all available tokens.
61 * An array of token types to use.
63 * An array with available tokens
65 public function getAvailableTokens(array $types = array()) {
66 $info = \Drupal::token()->getInfo();
67 $available = array_intersect_key($info['tokens'], array_flip($types));