metatag_views.tokens.inc 2.7 KB

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