views.tokens.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Token integration for the views module.
  5. */
  6. use Drupal\Core\Render\BubbleableMetadata;
  7. /**
  8. * Implements hook_token_info().
  9. */
  10. function views_token_info() {
  11. $info['types']['view'] = [
  12. 'name' => t('View', [], ['context' => 'View entity type']),
  13. 'description' => t('Tokens related to views.'),
  14. 'needs-data' => 'view',
  15. ];
  16. $info['tokens']['view']['label'] = [
  17. 'name' => t('Label'),
  18. 'description' => t('The label of the view.'),
  19. ];
  20. $info['tokens']['view']['description'] = [
  21. 'name' => t('Description'),
  22. 'description' => t('The description of the view.'),
  23. ];
  24. $info['tokens']['view']['id'] = [
  25. 'name' => t('ID'),
  26. 'description' => t('The machine-readable ID of the view.'),
  27. ];
  28. $info['tokens']['view']['title'] = [
  29. 'name' => t('Title'),
  30. 'description' => t('The title of current display of the view.'),
  31. ];
  32. $info['tokens']['view']['url'] = [
  33. 'name' => t('URL'),
  34. 'description' => t('The URL of the view.'),
  35. 'type' => 'url',
  36. ];
  37. $info['tokens']['view']['base-table'] = [
  38. 'name' => t('Base table'),
  39. 'description' => t('The base table used for this view.'),
  40. ];
  41. $info['tokens']['view']['base-field'] = [
  42. 'name' => t('Base field'),
  43. 'description' => t('The base field used for this view.'),
  44. ];
  45. $info['tokens']['view']['total-rows'] = [
  46. 'name' => t('Total rows'),
  47. 'description' => t('The total amount of results returned from the view. The current display will be used.'),
  48. ];
  49. $info['tokens']['view']['items-per-page'] = [
  50. 'name' => t('Items per page'),
  51. 'description' => t('The number of items per page.'),
  52. ];
  53. $info['tokens']['view']['current-page'] = [
  54. 'name' => t('Current page'),
  55. 'description' => t('The current page of results the view is on.'),
  56. ];
  57. $info['tokens']['view']['page-count'] = [
  58. 'name' => t('Page count'),
  59. 'description' => t('The total page count.'),
  60. ];
  61. return $info;
  62. }
  63. /**
  64. * Implements hook_tokens().
  65. */
  66. function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  67. $url_options = ['absolute' => TRUE];
  68. if (isset($options['language'])) {
  69. $url_options['language'] = $options['language'];
  70. }
  71. $replacements = [];
  72. if ($type == 'view' && !empty($data['view'])) {
  73. /** @var \Drupal\views\ViewExecutable $view */
  74. $view = $data['view'];
  75. $bubbleable_metadata->addCacheableDependency($view->storage);
  76. foreach ($tokens as $name => $original) {
  77. switch ($name) {
  78. case 'label':
  79. $replacements[$original] = $view->storage->label();
  80. break;
  81. case 'description':
  82. $replacements[$original] = $view->storage->get('description');
  83. break;
  84. case 'id':
  85. $replacements[$original] = $view->storage->id();
  86. break;
  87. case 'title':
  88. $title = $view->getTitle();
  89. $replacements[$original] = $title;
  90. break;
  91. case 'url':
  92. try {
  93. if ($url = $view->getUrl()) {
  94. $replacements[$original] = $url->setOptions($url_options)
  95. ->toString();
  96. }
  97. }
  98. catch (\InvalidArgumentException $e) {
  99. // The view has no URL so we leave the value empty.
  100. $replacements[$original] = '';
  101. }
  102. break;
  103. case 'base-table':
  104. $replacements[$original] = $view->storage->get('base_table');
  105. break;
  106. case 'base-field':
  107. $replacements[$original] = $view->storage->get('base_field');
  108. break;
  109. case 'total-rows':
  110. $replacements[$original] = (int) $view->total_rows;
  111. break;
  112. case 'items-per-page':
  113. $replacements[$original] = (int) $view->getItemsPerPage();
  114. break;
  115. case 'current-page':
  116. $replacements[$original] = (int) $view->getCurrentPage() + 1;
  117. break;
  118. case 'page-count':
  119. // If there are no items per page, set this to 1 for the division.
  120. $per_page = $view->getItemsPerPage() ?: 1;
  121. $replacements[$original] = max(1, (int) ceil($view->total_rows / $per_page));
  122. break;
  123. }
  124. }
  125. }
  126. return $replacements;
  127. }