-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews_autorefresh.module
139 lines (131 loc) · 4.34 KB
/
views_autorefresh.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
<?php
/**
* @file
* Contains hooks for the views_autorefresh module.
*/
use Drupal\views\ViewExecutable;
use Drupal\Core\Link;
use Drupal\views\Views;
/**
* Helper function to return view's "timestamp" - either real timestamp or max primary key in view rows.
*/
function views_autorefresh_get_timestamp($view) {
$autorefresh = views_autorefresh_get_settings($view);
if (empty($autorefresh)) {
return FALSE;
}
if (empty($autorefresh['incremental'])) {
return time();
}
foreach ($view->argument as $argument) {
$handler = Views::handlerManager($argument->field)->getHandler($argument->table, 'argument');
if ($handler->definition['handler'] == 'views_autorefresh_handler_argument_date') {
return time();
}
elseif ($handler->definition['handler'] == 'views_autorefresh_handler_argument_base') {
// Find the max nid/uid/... of the result set.
$max_id = array_reduce($view->result, function ($max_id, $row) use ($view) {
return max($max_id, $row->{$view->base_field});
}, ~PHP_INT_MAX);
return $max_id === ~PHP_INT_MAX ? FALSE : $max_id;
}
}
return FALSE;
}
/**
* Implementation of hook_theme().
*/
function views_autorefresh_theme($existing, $type, $theme, $path) {
return array(
'views_autorefresh_link' => array(
'variables' => array(
'interval' => NULL,
'timestamp' => NULL,
'ping' => NULL,
'incremental' => NULL,
'view' => NULL,
'nodejs' => NULL,
),
),
);
}
/**
* Implements default pre-processing for views_autorefresh_link.
*/
function template_preprocess_views_autorefresh_link(&$variables) {
$view = $variables['view'];
$exposed_input = $view->getExposedInput();
// Remove the ajax page state from the query.
if (isset($exposed_input['ajax_page_state'])) {
unset($exposed_input['ajax_page_state']);
}
$link = Link::createFromRoute(t('Refresh manually'), '<current>', [], ['query' => $exposed_input])->toRenderable();
$variables['content'] = [
'#type' => 'container',
'#attributes' => ['class' => ['auto-refresh']],
'#attached' => [
'library' => ['views_autorefresh/views_autorefresh'],
'drupalSettings' => [
'viewsAutorefresh' => [
$view->id() . '-' . $view->current_display => [
'interval' => $variables['interval'],
'timestamp' => $variables['timestamp'],
//'ping' => $variables['ping'],
'incremental' => $variables['incremental'],
//'nodejs' => $variables['nodejs'],
]
]
],
],
'link' => $link,
];
}
/**
* Helper function to get autorefresh settings.
*/
function views_autorefresh_get_settings($view) {
if (isset($view->display_handler->handlers['header']['autorefresh'])) {
return $view->display_handler->handlers['header']['autorefresh']->options;
}
elseif (isset($view->display_handler->handlers['footer']['autorefresh'])) {
return $view->display_handler->handlers['footer']['autorefresh']->options;
}
foreach ($view->displayHandlers as $display) {
if (isset($display->getOption('header')['autorefresh'])) {
return $display->getOption('header')['autorefresh'];
}
elseif (isset($display->getOption('footer')['autorefresh'])) {
return $display->getOption('footer')['autorefresh'];
}
}
return NULL;
}
/**
* Implements hook_views_pre_render().
*
* Reset information about first display instead of the second display (incremental approach only)
* FIXME: Check if it's necessary to reset any further information
*/
function views_autorefresh_views_pre_render(ViewExecutable $view) {
if (isset($_REQUEST['original_view_data']) &&
!empty($_REQUEST['original_view_data']['view_display_id']) &&
isset($_REQUEST['original_view_data']['view_dom_id']) &&
$_REQUEST['original_view_data']['view_dom_id'] == $view->dom_id
) {
$view->current_display = $_REQUEST['original_view_data']['view_display_id'];
}
}
/**
* Refreshes a view through nodejs.
*/
function views_autorefresh_nodejs_refresh($views, $context) {
foreach ($views as $view_name) {
$message = (object) array(
'channel' => 'views_autorefresh_' . $view_name,
'callback' => 'viewsAutoRefresh',
'view_name' => $view_name,
);
drupal_alter('views_autorefresh_nodejs_message', $message, $context);
nodejs_send_content_channel_message($message);
}
}