MatcherTokensTrait.php in Linkit 8.5
File
src/MatcherTokensTrait.php
View source
<?php
namespace Drupal\linkit;
trait MatcherTokensTrait {
public function insertTokenList(array &$form, array $types = []) {
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$form['metadata']['token_tree'] = [
'#theme' => 'token_tree_link',
'#token_types' => $types,
'#dialog' => TRUE,
'#weight' => 10,
];
}
else {
$token_items = [];
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['metadata']['tokens'] = [
'#type' => 'details',
'#title' => t('Available tokens'),
'#weight' => 10,
];
$form['metadata']['tokens']['list'] = [
'#theme' => 'item_list',
'#items' => $token_items,
];
}
}
}
public function getAvailableTokens(array $types = []) {
$info = \Drupal::token()
->getInfo();
$available = array_intersect_key($info['tokens'], array_flip($types));
return $available;
}
}