views.tokens.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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'] = array(
  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'] = array(
  17. 'name' => t('Label'),
  18. 'description' => t('The label of the view.'),
  19. );
  20. $info['tokens']['view']['description'] = array(
  21. 'name' => t('Description'),
  22. 'description' => t('The description of the view.'),
  23. );
  24. $info['tokens']['view']['id'] = array(
  25. 'name' => t('ID'),
  26. 'description' => t('The machine-readable ID of the view.'),
  27. );
  28. $info['tokens']['view']['title'] = array(
  29. 'name' => t('Title'),
  30. 'description' => t('The title of current display of the view.'),
  31. );
  32. $info['tokens']['view']['url'] = array(
  33. 'name' => t('URL'),
  34. 'description' => t('The URL of the view.'),
  35. 'type' => 'url',
  36. );
  37. $info['tokens']['view']['base-table'] = array(
  38. 'name' => t('Base table'),
  39. 'description' => t('The base table used for this view.'),
  40. );
  41. $info['tokens']['view']['base-field'] = array(
  42. 'name' => t('Base field'),
  43. 'description' => t('The base field used for this view.'),
  44. );
  45. $info['tokens']['view']['total-rows'] = array(
  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'] = array(
  50. 'name' => t('Items per page'),
  51. 'description' => t('The number of items per page.'),
  52. );
  53. $info['tokens']['view']['current-page'] = array(
  54. 'name' => t('Current page'),
  55. 'description' => t('The current page of results the view is on.'),
  56. );
  57. $info['tokens']['view']['page-count'] = array(
  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 = array('absolute' => TRUE);
  68. if (isset($options['language'])) {
  69. $url_options['language'] = $options['language'];
  70. }
  71. $replacements = array();
  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. if ($url = $view->getUrl()) {
  93. $replacements[$original] = $url->setOptions($url_options)
  94. ->toString();
  95. }
  96. break;
  97. case 'base-table':
  98. $replacements[$original] = $view->storage->get('base_table');
  99. break;
  100. case 'base-field':
  101. $replacements[$original] = $view->storage->get('base_field');
  102. break;
  103. case 'total-rows':
  104. $replacements[$original] = count($view->result);
  105. break;
  106. case 'items-per-page':
  107. $replacements[$original] = (int) $view->getItemsPerPage();
  108. break;
  109. case 'current-page':
  110. $replacements[$original] = (int) $view->getCurrentPage() + 1;
  111. break;
  112. case 'page-count':
  113. // If there are no items per page, set this to 1 for the division.
  114. $per_page = $view->getItemsPerPage() ?: 1;
  115. $replacements[$original] = max(1, (int) ceil(count($view->result) / $per_page));
  116. break;
  117. }
  118. }
  119. }
  120. return $replacements;
  121. }