Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / token.pages.inc
1 <?php
2
3 /**
4  * @file
5  * User page callbacks for the token module.
6  */
7 use Drupal\Component\Serialization\Json;
8 use Drupal\Component\Utility\DiffArray;
9 use Drupal\Core\Link;
10 use Drupal\Core\Url;
11
12 /**
13  * Theme a link to a token tree shown as a dialog.
14  */
15 function template_preprocess_token_tree_link(&$variables) {
16   if (empty($variables['text'])) {
17     $variables['text'] = t('Browse available tokens.');
18   }
19
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';
24
25   $tree_variables = [
26     'token_types' => [],
27     'global_types' => TRUE,
28     'click_insert' => TRUE,
29     'show_restricted' => FALSE,
30     'show_nested' => FALSE,
31     'recursion_limit' => 3,
32   ];
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'] = [];
37   }
38   $variables['options']['query']['options'] += $query_options;
39
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']);
44
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',
50       'width' => 600,
51       'height' => 400,
52       'position' => ['my' => 'right bottom', 'at' => 'right-10 bottom-10'],
53       'draggable' => TRUE,
54       'autoResize' => FALSE,
55     ]),
56   ];
57
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'];
61 }