views.tokens.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Token integration for the views module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function views_token_info() {
  10. $info['types']['view'] = array(
  11. 'name' => t('View'),
  12. 'description' => t('Tokens related to views.'),
  13. 'needs-data' => 'view',
  14. );
  15. $info['tokens']['view']['name'] = array(
  16. 'name' => t('Name'),
  17. 'description' => t('The human-readable name of the view.'),
  18. );
  19. $info['tokens']['view']['description'] = array(
  20. 'name' => t('Description'),
  21. 'description' => t('The description of the view.'),
  22. );
  23. $info['tokens']['view']['machine-name'] = array(
  24. 'name' => t('Machine name'),
  25. 'description' => t('The machine-readable name of the view.'),
  26. );
  27. $info['tokens']['view']['title'] = array(
  28. 'name' => t('Title'),
  29. 'description' => t('The title of current display of the view.'),
  30. );
  31. $info['tokens']['view']['url'] = array(
  32. 'name' => t('URL'),
  33. 'description' => t('The URL of the view.'),
  34. 'type' => 'url',
  35. );
  36. return $info;
  37. }
  38. /**
  39. * Implements hook_tokens().
  40. */
  41. function views_tokens($type, $tokens, array $data = array(), array $options = array()) {
  42. $url_options = array('absolute' => TRUE);
  43. if (isset($options['language'])) {
  44. $url_options['language'] = $options['language'];
  45. }
  46. $sanitize = !empty($options['sanitize']);
  47. $langcode = isset($options['language']) ? $options['language']->language : NULL;
  48. $replacements = array();
  49. if ($type == 'view' && !empty($data['view'])) {
  50. $view = $data['view'];
  51. foreach ($tokens as $name => $original) {
  52. switch ($name) {
  53. case 'name':
  54. $replacements[$original] = $sanitize ? check_plain($view->human_name) : $view->human_name;
  55. break;
  56. case 'description':
  57. $replacements[$original] = $sanitize ? check_plain($view->description) : $view->description;
  58. break;
  59. case 'machine-name':
  60. $replacements[$original] = $view->name;
  61. break;
  62. case 'title':
  63. $title = $view->get_title();
  64. $replacements[$original] = $sanitize ? check_plain($title) : $title;
  65. break;
  66. case 'url':
  67. if ($path = $view->get_url()) {
  68. $replacements[$original] = url($path, $url_options);
  69. }
  70. break;
  71. }
  72. }
  73. // [view:url:*] nested tokens. This only works if Token module is installed.
  74. if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
  75. if ($path = $view->get_url()) {
  76. $replacements += token_generate('url', $url_tokens, array('path' => $path), $options);
  77. }
  78. }
  79. }
  80. return $replacements;
  81. }