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