views.tokens.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. $replacements = array();
  48. if ($type == 'view' && !empty($data['view'])) {
  49. $view = $data['view'];
  50. foreach ($tokens as $name => $original) {
  51. switch ($name) {
  52. case 'name':
  53. $replacements[$original] = $sanitize ? check_plain($view->human_name) : $view->human_name;
  54. break;
  55. case 'description':
  56. $replacements[$original] = $sanitize ? check_plain($view->description) : $view->description;
  57. break;
  58. case 'machine-name':
  59. $replacements[$original] = $view->name;
  60. break;
  61. case 'title':
  62. $title = $view->get_title();
  63. $replacements[$original] = $sanitize ? check_plain($title) : $title;
  64. break;
  65. case 'url':
  66. if ($path = $view->get_url()) {
  67. $replacements[$original] = url($path, $url_options);
  68. }
  69. break;
  70. }
  71. }
  72. // [view:url:*] nested tokens. This only works if Token module is installed.
  73. if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
  74. if ($path = $view->get_url()) {
  75. $replacements += token_generate('url', $url_tokens, array('path' => $path), $options);
  76. }
  77. }
  78. }
  79. return $replacements;
  80. }