-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetree_views.module
394 lines (344 loc) · 14.7 KB
/
etree_views.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* @file
* Contains etree_views.module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Drupal\etree_views\handlers\ETreeViewsData;
/**
* Implements hook_help().
*/
function etree_views_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the etree_views module.
case 'help.page.etree_views':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('ETree views components (fields, filter and etc)') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function etree_views_theme() {
$theme = [];
$theme['views_view_etree'] = [
'variables' => [
'view_array' => [],
'view' => NULL,
'rows' => [],
'header' => [],
'footer' => [],
'sidebar' => [],
'empty' => [],
'exposed' => [],
'more' => [],
'feed_icons' => [],
'pager' => [],
'title' => '',
'attachment_before' => [],
'attachment_after' => [],
],
];
return $theme;
}
/**
* Prepares variables for view templates.
*
* Default template: views-view.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The ViewExecutable object.
*/
function template_preprocess_views_view_etree(&$variables) {
$view = $variables['view'];
$id = $view->storage->id();
$variables['css_name'] = Html::cleanCssIdentifier($id);
$variables['id'] = $id;
$variables['display_id'] = $view->current_display;
// Override the title to be empty by default. For example, if viewing a page
// view, 'title' will already be populated in $variables. This can still be
// overridden to use a title when needed. See views_ui_preprocess_views_view()
// for an example of this.
$variables['title'] = '';
$css_class = $view->display_handler->getOption('css_class');
if (!empty($css_class)) {
// Views uses its own sanitization method. This is preserved to keep
// backwards compatibility.
// @todo https://www.drupal.org/project/drupal/issues/2977950 Decide what to
// do with the backwards compatibility layer.
$bc_classes = explode(' ', preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class));
// Sanitize the classes using the classes using the proper API.
$sanitized_classes = array_map('\Drupal\Component\Utility\Html::cleanCssIdentifier', explode(' ', $css_class));
$view_classes = array_unique(array_merge($bc_classes, $sanitized_classes));
// Merge the view display classes into any existing classes if they exist.
$variables['attributes']['class'] = !empty($variables['attributes']['class']) ? array_merge($variables['attributes']['class'], $view_classes) : $view_classes;
$variables['css_class'] = implode(' ', $view_classes);
}
// contextual_preprocess() only works on render elements, and since this theme
// hook is not for a render element, contextual_preprocess() falls back to the
// first argument and checks if that is a render element. The first element is
// view_array. However, view_array does not get set anywhere, but since we do
// have access to the View object, we can also access the View object's
// element, which is a render element that does have #contextual_links set if
// the display supports it. Doing this allows contextual_preprocess() to
// access this theme hook's render element, and therefore allows this template
// to have contextual links.
// @see views_theme()
$variables['view_array'] = $variables['view']->element;
// Attachments are always updated with the outer view, never by themselves,
// so they do not have dom ids.
if (empty($view->is_attachment)) {
// Our JavaScript needs to have some means to find the HTML belonging to
// this view.
//
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique"
// identifier for each view. This identifier is written to both
// drupalSettings and the DIV wrapper.
$variables['dom_id'] = $view->dom_id;
}
}
/**
* Prepares variables for views table templates.
*
* Default template: views-view-table.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_etree_style(&$variables) {
$view = $variables['view'];
// We need the raw data for this grouping, which is passed in
// as $variables['rows'].
// However, the template also needs to use for the rendered fields. We
// therefore swap the raw data out to a new variable and reset $variables['rows']
// so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $variables['result'] = $variables['rows'];
$variables['rows'] = [];
$variables['header'] = [];
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$fields = &$view->field;
$columns = $handler->sanitizeColumns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
// A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE;
// For the actual site we want to not render full URLs, because this would
// make pagers cacheable per URL, which is problematic in blocks, for example.
// For the actual live preview though the javascript relies on properly
// working URLs.
$route_name = !empty($view->live_preview) ? '<current>' : '<none>';
$query = tablesort_get_query_parameters();
if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input;
}
// A boolean to store whether the table's header has any labels.
$has_header_labels = FALSE;
foreach ($columns as $field => $column) {
// Create a second variable so we can easily find what fields we have and
// what the CSS classes should be.
$variables['fields'][$field] = Html::cleanCssIdentifier($field);
if ($active == $field) {
$variables['fields'][$field] .= ' is-active';
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
$label = !empty($fields[$field]) ? $fields[$field]->label() : '';
if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
$variables['header'][$field]['content'] = $label;
}
else {
$initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
if ($active == $field) {
$initial = ($order == 'asc') ? 'desc' : 'asc';
}
$title = t('sort by @s', ['@s' => $label]);
if ($active == $field) {
$variables['header'][$field]['sort_indicator'] = [
'#theme' => 'tablesort_indicator',
'#style' => $initial,
];
}
$query['order'] = $field;
$query['sort'] = $initial;
$link_options = [
'query' => $query,
];
$url = new Url($route_name, [], $link_options);
$variables['header'][$field]['url'] = $url->toString();
$variables['header'][$field]['content'] = $label;
$variables['header'][$field]['title'] = $title;
}
$variables['header'][$field]['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set up the header label class.
$variables['header'][$field]['attributes'] = [];
$class = $fields[$field]->elementLabelClasses(0);
if ($class) {
$variables['header'][$field]['attributes']['class'][] = $class;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
$responsive = TRUE;
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['header'][$field]['attributes']['class'][] = Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if ($variables['header'][$field]['content']) {
$element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
if ($element_label_type) {
$variables['header'][$field]['wrapper_element'] = $element_label_type;
}
// Improves accessibility of complex tables.
$variables['header'][$field]['attributes']['id'] = Html::getUniqueId('view-' . $field . '-table-column');
}
// Check if header label is not empty.
if (!empty($variables['header'][$field]['content'])) {
$has_header_labels = TRUE;
}
$variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['fields'][$field] .= ' ' . Html::cleanCssIdentifier($options['info'][$field]['align']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Skip building the attributes and content if the field is to be excluded
// from the display.
if (!empty($fields[$field]->options['exclude'])) {
continue;
}
// Reference to the column in the loop to make the code easier to read.
$column_reference =& $variables['rows'][$num]['columns'][$column];
$column_reference['default_classes'] = $fields[$field]->options['element_default_classes'];
// Set the field key to the column so it can be used for adding classes
// in a template.
$column_reference['fields'][] = $variables['fields'][$field];
// Add field classes.
if (!isset($column_reference['attributes'])) {
$column_reference['attributes'] = [];
}
if ($classes = $fields[$field]->elementClasses($num)) {
$column_reference['attributes']['class'][] = $classes;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
}
// Improves accessibility of complex tables.
if (isset($variables['header'][$field]['attributes']['id'])) {
$column_reference['attributes']['headers'] = [$variables['header'][$field]['attributes']['id']];
}
if (!empty($fields[$field])) {
$field_output = $handler->getField($num, $field);
$column_reference['wrapper_element'] = $fields[$field]->elementType(TRUE, TRUE);
if (!isset($column_reference['content'])) {
$column_reference['content'] = [];
}
// Only bother with separators and stuff if the field shows up.
// Place the field into the column, along with an optional separator.
if (trim($field_output) != '') {
if (!empty($column_reference['content']) && !empty($options['info'][$column]['separator'])) {
$column_reference['content'][] = [
'separator' => ['#markup' => $options['info'][$column]['separator']],
'field_output' => ['#markup' => $field_output],
];
}
else {
$column_reference['content'][] = [
'field_output' => ['#markup' => $field_output],
];
}
}
}
$column_reference['attributes'] = new Attribute($column_reference['attributes']);
}
// Remove columns if the "empty_column" option is checked and the
// field is empty.
if (!empty($options['info'][$field]['empty_column'])) {
$empty = TRUE;
foreach ($variables['rows'] as $columns) {
$empty &= empty($columns['columns'][$column]['content']);
}
if ($empty) {
foreach ($variables['rows'] as &$column_items) {
unset($column_items['columns'][$column]);
}
unset($variables['header'][$column]);
}
}
}
// Hide table header if all labels are empty.
if (!$has_header_labels) {
$variables['header'] = [];
}
foreach ($variables['rows'] as $num => $row) {
$variables['rows'][$num]['attributes'] = [];
if ($row_class = $handler->getRowClass($num)) {
$variables['rows'][$num]['attributes']['class'][] = $row_class;
}
$variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
}
if (empty($variables['rows']) && !empty($options['empty_table'])) {
$build = $view->display_handler->renderArea('empty');
$variables['rows'][0]['columns'][0]['content'][0]['field_output'] = $build;
$variables['rows'][0]['attributes'] = new Attribute(['class' => ['odd']]);
// Calculate the amounts of rows with output.
$variables['rows'][0]['columns'][0]['attributes'] = new Attribute([
'colspan' => count($variables['header']),
'class' => ['views-empty'],
]);
}
$variables['sticky'] = FALSE;
if (!empty($options['sticky'])) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader';
$variables['sticky'] = TRUE;
}
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$variables['caption'] = ['#markup' => $handler->options['caption']];
$variables['caption_needed'] = TRUE;
}
elseif (!empty($variables['title'])) {
$variables['caption'] = ['#markup' => $variables['title']];
$variables['caption_needed'] = TRUE;
}
else {
$variables['caption'] = '';
$variables['caption_needed'] = FALSE;
}
$variables['summary'] = $handler->options['summary'];
$variables['description'] = $handler->options['description'];
$variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']);
$variables['responsive'] = FALSE;
// If the table has headers and it should react responsively to columns hidden
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (isset($variables['header']) && $responsive) {
$variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive';
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$variables['responsive'] = TRUE;
}
}
/**
* Implements hook_entity_type_alter().
*/
function etree_views_entity_type_alter(array &$entity_types) {
$entity_types['etree']->setHandlerClass('views_data', ETreeViewsData::class);
}