forked from backdrop-contrib/block_refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_refresh.module
More file actions
executable file
·277 lines (255 loc) · 9.92 KB
/
Copy pathblock_refresh.module
File metadata and controls
executable file
·277 lines (255 loc) · 9.92 KB
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
<?php
/**
* @file
* Allows blocks to automatically refresh their content.
*/
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC', FALSE); // autorefresh disabled by default
define('BLOCK_REFRESH_DEFAULT_MANUAL', FALSE); // manual refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_INIT', FALSE); // inital refresh disabled by default
define('BLOCK_REFRESH_DEFAULT_BYPASS_PAGE_CACHE', FALSE); // page cache bypass disabled by default
define('BLOCK_REFRESH_DEFAULT_BYPASS_EXTERNAL_CACHE', ''); // external cache bypass disabled by default
define('BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER', 120); // default refreshes every two minutes, if enabled
/**
* Implements hook_block_view_alter().
*
* Adds the jquery to refresh blocks automatically.
*/
function block_refresh_block_view_alter(&$data, $block) {
if (array_key_exists('block_refresh', $block->settings) && !empty($block->settings['block_refresh'])) {
// Add extra settings.
$element = backdrop_clean_css_identifier('block-' . $block->module . '-' . $block->delta);
$block->settings['block_refresh']['element'] = $element;
$block->settings['block_refresh']['module'] = $block->module;
$block->settings['block_refresh']['delta'] = $block->delta;
$block->settings['block_refresh']['uuid'] = $block->uuid;
// Add assets to the page.
$path = backdrop_get_path('module', 'block_refresh');
backdrop_add_js($path . '/js/block_refresh.js', array('scope' => 'footer'));
backdrop_add_css($path . '/css/block_refresh.css');
backdrop_add_js(array(
'block_refresh' => array(
'settings' => $block->settings['block_refresh'],
'args' => arg(),
'query' => block_refresh_get_query_as_string(),
'cleanUrl' => (bool) config_get('system.core', 'clean_url'),
)
), 'setting');
/* @todo Find out why #attached does not work here.
$data['#attached'] = array(
'css' => array($path . '/css/block_refresh.css'),
'js' => array($path . '/js/block_refresh.js' => array('scope' => 'footer')),
);
$data['#attached']['js'][] = array(
'data' => array('block_refresh' => $block_refresh_settings),
'type' => 'setting',
);
*/
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds a 'Block Refresh' settings fieldset to the block admin form.
*/
function block_refresh_form_layout_block_configure_form_alter(&$form, $form_state) {
$form['block_refresh'] = array(
'#type' => 'fieldset',
'#title' => t('Refresh settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
if (($form_state['block']->module == 'system' && $form_state['block']->delta == 'main')) {
$form['block_refresh']['#collapsed'] = FALSE;
$form['block_refresh']['no'] = array(
'#type' => 'markup',
'#markup' => t('The main page content block cannot be refreshed.'),
);
}
else {
block_refresh_settings_form_elements($form, $form_state);
// Call our submit handler before layout's.
array_unshift($form['#submit'], 'block_refresh_submit');
}
}
/**
* Helper function: Generates the form elements for block refresh settings.
*
* @param $form
* The block settings form.
*/
function block_refresh_settings_form_elements(&$form, $form_state) {
$settings = array();
if (isset($form_state['block']->settings['block_refresh'])) {
$settings = $form_state['block']->settings['block_refresh'];
$form['block_refresh']['#collapsed'] = FALSE;
}
$timer_setting = isset($settings['timer']) ? $settings['timer'] : BLOCK_REFRESH_DEFAULT_AUTOMATIC_TIMER;
$form['block_refresh']['auto'] = array(
'#type' => 'checkbox',
'#title' => t('Refresh automatically'),
'#description' => t('When checked, the content of this block will refresh automatically every @timer seconds (defined below).', array('@timer' => $timer_setting)),
'#default_value' => isset($settings['auto']) ? $settings['auto'] : BLOCK_REFRESH_DEFAULT_AUTOMATIC,
);
$form['block_refresh']['timer'] = array(
'#type' => 'number',
'#title' => t('Block refresh interval'),
'#field_suffix' => t('seconds'),
'#default_value' => $timer_setting,
'#states' => array(
'visible' => array(
':input[name="block_refresh[auto]"]' => array('checked' => TRUE),
),
),
);
$form['block_refresh']['init'] = array(
'#type' => 'checkbox',
'#title' => t('Refresh on page load'),
'#description' => t('When checked, the content of this block will be refreshed every time the page loads.'),
'#default_value' => isset($settings['init']) ? $settings['init'] : BLOCK_REFRESH_DEFAULT_INIT,
);
$form['block_refresh']['manual'] = array(
'#type' => 'checkbox',
'#title' => t('Refresh manually'),
'#description' => t('When checked, the content of this block may be refreshed manually by clicking a button in the block\'s header.'),
'#default_value' => isset($settings['manual']) ? $settings['manual'] : BLOCK_REFRESH_DEFAULT_MANUAL,
);
$form['block_refresh']['cache'] = array(
'#type' => 'fieldset',
'#title' => t('Cache settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['block_refresh']['cache']['bypass_page_cache'] = array(
'#type' => 'checkbox',
'#title' => t('Bypass page cache'),
'#description' => t('When checked, the refreshed content of this block will bypass the page cache. If stale content is being served to you due to caching, try checking this box. <em>Warning! this can have a performance impact</em>.'),
'#default_value' => isset($settings['bypass_page_cache']) ? $settings['bypass_page_cache'] : BLOCK_REFRESH_DEFAULT_BYPASS_PAGE_CACHE,
);
$form['block_refresh']['cache']['bypass_external_cache'] = array(
'#type' => 'number',
'#title' => t('External cache max age'),
'#field_suffix' => t('seconds'),
'#description' => t('If you wish to override the max age of refreshed data served from an external cache (eg Varnish), enter a value here. Leave blank to use your sitewide default value.'),
'#default_value' => isset($settings['bypass_external_cache']) ? $settings['bypass_external_cache'] : BLOCK_REFRESH_DEFAULT_BYPASS_EXTERNAL_CACHE,
);
}
/**
* Submit handler for for block_refresh_menu().
*
* Handles form submissions on the block configuration page.
*/
function block_refresh_submit($form, &$form_state) {
$settings = array();
if (isset($form_state['block']->settings['block_refresh'])) {
$settings = $form_state['block']->settings['block_refresh'];
}
if (($form_state['values']['block_refresh']['auto'] == 0) &&
($form_state['values']['block_refresh']['manual'] == 0) &&
($form_state['values']['block_refresh']['init'] == 0)) {
// Remove all block refresh settings if refresh is disabled.
$form_state['block']->settings['block_refresh'] = array();
}
else {
if (is_null($settings)) {
$settings = array();
}
foreach ($form_state['values']['block_refresh'] as $key => $value) {
// Flatten all refresh settings into an one-dimensional array.
if (is_array($value)) {
foreach ($value as $inner_key => $inner_value) {
$settings[$inner_key] = $inner_value;
}
unset($form_state['values']['block_refresh'][$key]);
unset($settings[$key]);
}
else {
$settings[$key] = $value;
}
}
$form_state['block']->settings['block_refresh'] = $settings;
}
}
/**
* Implements hook_menu().
*/
function block_refresh_menu() {
$items = array();
$items['block_refresh'] = array(
'title' => 'Block refresh block content',
'page callback' => 'block_refresh_block_content',
'page arguments' => array(1, 2, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback for /block_refresh/[module]/[delta]/[uuid].
*
* Displays the block content without any other page information.
*
* @param string $module
* Module that provided the block.
*
* @param string $delta
* Identifies which block provided by a given module.
*
* @param string $uuid
* Identifies which instance of the block is to be refreshed.
*
* @return string
* Rendered content of the block, or 404 if there was a problem.
*/
function block_refresh_block_content($module = NULL, $delta = NULL, $uuid = NULL) {
// 404 if there is a request directly to /block_refresh or UUID is not set.
if (!isset($module) || !isset($delta) || !isset($uuid)) {
backdrop_not_found();
}
$menu_router_item = menu_get_item();
$layout = layout_get_layout_by_path(NULL, $menu_router_item);
if (isset($layout->content[$uuid])) {
$settings = $layout->content[$uuid]->settings['block_refresh'];
// If automatic refresh AND manual refresh are both disabled...
if (empty($settings) || (!$settings['auto'] && !$settings['manual'] && !$settings['init'])) {
backdrop_not_found();
}
// Bypass page cache if set.
if (isset($settings['bypass_page_cache']) && $settings['bypass_page_cache'] == 1) {
$GLOBALS['conf']['cache'] = FALSE;
}
// Override external cache max age if set.
if (isset($settings['bypass_external_cache']) && $settings['bypass_external_cache'] != '') {
backdrop_add_http_header('Cache-Control', 'public, max-age=' . intval($settings['bypass_external_cache']));
}
$output = $layout->content[$uuid]->getContent();
print $output;
}
else {
$message = 'A block with UUID :uuid was not found in the active layout.';
$replacements = array(':uuid' => $uuid);
watchdog('block_refresh', $message, $replacements, WATCHDOG_WARNING);
}
}
/**
* Private function: Gets all $_GET values (except q and render).
*
* @return string
* A query string representing the arguments.
*/
function block_refresh_get_query_as_string() {
$variables = $_GET;
if (!empty($variables['q'])) {
unset($variables['q']);
}
if (!empty($variables['render'])) {
unset($variables['render']);
}
if (count($variables) > 0) {
$querystring = '?' . http_build_query($variables);
return $querystring;
}
else {
return '';
}
}