5 * User page callbacks for the token module.
7 use Drupal\Component\Serialization\Json;
8 use Drupal\Component\Utility\DiffArray;
13 * Theme a link to a token tree shown as a dialog.
15 function template_preprocess_token_tree_link(&$variables) {
16 if (empty($variables['text'])) {
17 $variables['text'] = t('Browse available tokens.');
20 $variables['#attached']['library'][] = 'core/drupal.dialog.ajax';
21 $variables['#attached']['library'][] = 'token/token';
22 $variables['options']['attributes']['class'][] = 'token-dialog';
23 $variables['options']['attributes']['class'][] = 'use-ajax';
27 'global_types' => TRUE,
28 'click_insert' => TRUE,
29 'show_restricted' => FALSE,
30 'show_nested' => FALSE,
31 'recursion_limit' => 3,
33 $query_options = array_intersect_key($variables, $tree_variables);
34 $query_options = DiffArray::diffAssocRecursive($query_options, $tree_variables);
35 if (!isset($variables['options']['query']['options'])) {
36 $variables['options']['query']['options'] = [];
38 $variables['options']['query']['options'] += $query_options;
40 // Because PHP converts query strings with arrays into a different syntax on
41 // the next request, the options have to be encoded with JSON in the query
42 // string so that we can reliably decode it for token comparison.
43 $variables['options']['query']['options'] = Json::encode($variables['options']['query']['options']);
45 // Set the token tree to open in a separate window.
46 $variables['options']['attributes'] += [
47 'data-dialog-type' => 'dialog',
48 'data-dialog-options' => json_encode([
49 'dialogClass' => 'token-tree-dialog',
52 'position' => ['my' => 'right bottom', 'at' => 'right-10 bottom-10'],
54 'autoResize' => FALSE,
58 $variables['link'] = Link::createFromRoute($variables['text'], 'token.tree', [], $variables['options'])->toRenderable();
59 $variables['url'] = new Url('token.tree', [], $variables['options']);
60 $variables['attributes'] = $variables['options']['attributes'];