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