5 * Token integration for the views module.
8 use Drupal\Core\Render\BubbleableMetadata;
11 * Implements hook_token_info().
13 function views_token_info() {
14 $info['types']['view'] = [
15 'name' => t('View', [], ['context' => 'View entity type']),
16 'description' => t('Tokens related to views.'),
17 'needs-data' => 'view',
19 $info['tokens']['view']['label'] = [
21 'description' => t('The label of the view.'),
23 $info['tokens']['view']['description'] = [
24 'name' => t('Description'),
25 'description' => t('The description of the view.'),
27 $info['tokens']['view']['id'] = [
29 'description' => t('The machine-readable ID of the view.'),
31 $info['tokens']['view']['title'] = [
33 'description' => t('The title of current display of the view.'),
35 $info['tokens']['view']['url'] = [
37 'description' => t('The URL of the view.'),
40 $info['tokens']['view']['base-table'] = [
41 'name' => t('Base table'),
42 'description' => t('The base table used for this view.'),
44 $info['tokens']['view']['base-field'] = [
45 'name' => t('Base field'),
46 'description' => t('The base field used for this view.'),
48 $info['tokens']['view']['total-rows'] = [
49 'name' => t('Total rows'),
50 'description' => t('The total amount of results returned from the view. The current display will be used.'),
52 $info['tokens']['view']['items-per-page'] = [
53 'name' => t('Items per page'),
54 'description' => t('The number of items per page.'),
56 $info['tokens']['view']['current-page'] = [
57 'name' => t('Current page'),
58 'description' => t('The current page of results the view is on.'),
60 $info['tokens']['view']['page-count'] = [
61 'name' => t('Page count'),
62 'description' => t('The total page count.'),
69 * Implements hook_tokens().
71 function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
72 $url_options = ['absolute' => TRUE];
73 if (isset($options['language'])) {
74 $url_options['language'] = $options['language'];
78 if ($type == 'view' && !empty($data['view'])) {
79 /** @var \Drupal\views\ViewExecutable $view */
80 $view = $data['view'];
82 $bubbleable_metadata->addCacheableDependency($view->storage);
84 foreach ($tokens as $name => $original) {
87 $replacements[$original] = $view->storage->label();
91 $replacements[$original] = $view->storage->get('description');
95 $replacements[$original] = $view->storage->id();
99 $title = $view->getTitle();
100 $replacements[$original] = $title;
105 if ($url = $view->getUrl()) {
106 $replacements[$original] = $url->setOptions($url_options)
110 catch (\InvalidArgumentException $e) {
111 // The view has no URL so we leave the value empty.
112 $replacements[$original] = '';
116 $replacements[$original] = $view->storage->get('base_table');
119 $replacements[$original] = $view->storage->get('base_field');
122 $replacements[$original] = (int) $view->total_rows;
124 case 'items-per-page':
125 $replacements[$original] = (int) $view->getItemsPerPage();
128 $replacements[$original] = (int) $view->getCurrentPage() + 1;
131 // If there are no items per page, set this to 1 for the division.
132 $per_page = $view->getItemsPerPage() ?: 1;
133 $replacements[$original] = max(1, (int) ceil($view->total_rows / $per_page));
139 return $replacements;