token.pages.inc 2.1 KB

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