generated from backdrop-contrib/module_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforward_revisions.module
349 lines (306 loc) · 10.3 KB
/
forward_revisions.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
<?php
/**
* @file Contains hook implementations and functions for this module.
*/
/**
* Implements hook_menu().
*/
function forward_revisions_menu() {
$items = [];
$items['node/%node/revisions/%/activate'] = [
'title' => 'Make a revision the current revision',
'load arguments' => [3],
'page callback' => 'backdrop_get_form',
'page arguments' => ['forward_revisions_activate_confirm', 1],
'access callback' => '_node_revision_access',
'access arguments' => [1, 'update'],
];
$items['node/%node/publish'] = [
'title' => 'Publish the node',
// 'load arguments' => [1],
'page callback' => 'backdrop_get_form',
'page arguments' => ['forward_revisions_publish_confirm', 1],
'access callback' => 'node_access',
'access arguments' => ['update', 1],
];
$items['node/%node/unpublish'] = [
'title' => 'Unpublish the node',
// 'load arguments' => [1],
'page callback' => 'backdrop_get_form',
'page arguments' => ['forward_revisions_unpublish_confirm', 1],
'access callback' => 'node_access',
'access arguments' => ['update', 1],
];
$items['node/%node/revisions/%/new_revision'] = array(
'title' => 'Create new revision',
'load arguments' => [3],
'page callback' => 'forward_revisions_new_revision',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'weight' => 0,
);
$items['node/%node/revisions/%/edit_revision'] = array(
'title' => 'Edit revision',
'load arguments' => [3],
'page callback' => 'forward_revisions_edit_revision',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('update', 1),
'weight' => 0,
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function forward_revisions_menu_alter(&$items) {
// Override node/%node/revisions.
$items['node/%node/revisions']['page callback'] = 'forward_revisions_node_revisions_overview';
}
/**
* Implements hook_admin_paths().
*/
function forward_revisions_admin_paths() {
if (config_get('system.core', 'node_admin_theme')) {
$paths = array(
'node/*/revisions/*/activate' => TRUE,
'node/*/publish' => TRUE,
'node/*/unpublish' => TRUE,
'node/*/revisions/*/new_revision' => TRUE,
'node/*/revisions/*/edit_revision' => TRUE,
);
return $paths;
}
else {
return array();
}
}
/**
* Provides a replacement for the node revisions page.
*/
function forward_revisions_node_revisions_overview(Node $node) {
module_load_include('inc', 'node', 'node.pages');
$build = node_revision_overview($node);
$build['actions']= [
'#type' => 'container',
'#weight' => -1,
'new_revision' => [
'#type' => 'link',
'#title' => 'Create new revision',
'#href' => 'node/' . $node->nid . '/revisions/' . $node->vid . '/new_revision',
'#attributes' => array('class' => 'button'),
],
];
$node_type_name = node_type_get_name($node);
if ($node->status == NODE_PUBLISHED) {
backdrop_set_message(t('This @node_type is published.', [
'@node_type' => $node_type_name,
]), 'info');
$build['actions']['unpublish'] = [
'#type' => 'link',
'#title' => 'Unpublish node',
'#href' => 'node/' . $node->nid . '/unpublish',
'#attributes' => array('class' => 'button'),
];
}
else {
backdrop_set_message(t('This @node_type is not published.', [
'@node_type' => $node_type_name,
]), 'warning');
$build['actions']['publish'] = [
'#type' => 'link',
'#title' => 'publish node',
'#href' => 'node/' . $node->nid . '/publish',
'#attributes' => array('class' => 'button'),
];
}
$revision_list = node_revision_list($node);
$key = 0;
foreach ($revision_list as $vid => $revision) {
$node_revision = node_load($node->nid, $vid);
// Edit the action links.
if ($vid != $node->vid) {
// Remove the Revert option.
unset($build['node_revisions_table']['#rows'][$key][1]['data']['#links']['revert']);
array_splice($build['node_revisions_table']['#rows'][$key][1]['data']['#links'], 0, 0, [
'edit_revision' => [
'title' => t('Edit Revision'),
'href' => 'node/' . $node->nid . '/revisions/' . $vid . '/edit_revision',
],
'activate' => [
'title' => t('Make current revision'),
'href' => 'node/' . $node->nid . '/revisions/' . $vid . '/activate',
],
'new_revision' => [
'title' => t('Create New Revision'),
'href' => 'node/' . $node->nid . '/revisions/' . $vid . '/new_revision',
],
]);
}
$key++;
}
return $build;
}
/**
* The confirmation form for activating a revision.
*/
function forward_revisions_activate_confirm($form, $form_state, $node_revision) {
$form['#node_revision'] = $node_revision;
return confirm_form($form, t('Are you sure you want to make the revision from %revision-date the current revision?', array(
'%revision-date' => format_date($node_revision->revision_timestamp),
)), 'node/' . $node_revision->nid . '/revisions', '', t('Save'), t('Cancel'));
}
/**
* Form submission handler for forward_revisions_activate_confirm().
*/
function forward_revisions_activate_confirm_submit($form, &$form_state) {
$node_revision = $form['#node_revision'];
$node_revision->setIsActiveRevision();
$node_revision->save();
backdrop_set_message(t('@type %title has been set as the active revision.', array(
'@type' => node_type_get_name($node_revision),
'%title' => $node_revision->title,
)));
$form_state['redirect'] = 'node/' . $node_revision->nid . '/revisions';
}
/**
* The confirmation form for publishing a draft revision.
*/
function forward_revisions_publish_confirm($form, $form_state, $node) {
$form['#node'] = $node;
return confirm_form($form, t('Are you sure you want to publish %node_title?', array(
'%node_title' => $node->title,
)), 'node/' . $node->nid . '/revisions', '', t('Publish'), t('Cancel'));
}
/**
* Form submission handler for forward_revisions_publish_confirm().
*/
function forward_revisions_publish_confirm_submit($form, &$form_state) {
$node = $form['#node'];
$node->status = NODE_PUBLISHED;
$node->save();
db_update('node_revision')
->fields(['status' => $node->status])
->condition('nid', $node->nid)
->execute();
backdrop_set_message(t('@type %title has been published.', array(
'@type' => node_type_get_name($node),
'%title' => $node->title,
)));
$form_state['redirect'] = 'node/' . $node->nid . '/revisions';
}
/**
* The confirmation form for publishing a draft revision.
*/
function forward_revisions_unpublish_confirm($form, $form_state, $node) {
$form['#node'] = $node;
return confirm_form($form, t('Are you sure you want to unpublish %node_title?', array(
'%node_title' => $node->title,
)), 'node/' . $node->nid . '/revisions', '', t('Unpublish'), t('Cancel'));
}
/**
* Form submission handler for forward_revisions_publish_confirm().
*/
function forward_revisions_unpublish_confirm_submit($form, &$form_state) {
$node = $form['#node'];
$node->status = NODE_NOT_PUBLISHED;
$node->save();
db_update('node_revision')
->fields(['status' => $node->status])
->condition('nid', $node->nid)
->execute();
backdrop_set_message(t('@type %title has been unpublished.', array(
'@type' => node_type_get_name($node),
'%title' => $node->title,
)));
$form_state['redirect'] = 'node/' . $node->nid . '/revisions';
}
/**
* Loads the node form for creating a new revision.
*/
function forward_revisions_new_revision(Node $node) {
module_load_include('inc', 'node', 'node.pages');
$type_name = node_type_get_name($node);
backdrop_set_title(t('New revision of @type %title', array(
'@type' => $type_name,
'%title' => $node->title,
)), PASS_THROUGH);
$form = backdrop_get_form($node->type . '_node_form', $node);
return $form;
}
/**
* Loads the node form for editing an existing revision.
*/
function forward_revisions_edit_revision(Node $node) {
module_load_include('inc', 'node', 'node.pages');
$type_name = node_type_get_name($node);
backdrop_set_title(t('Edit draft of @type %title', array(
'@type' => $type_name,
'%title' => $node->title,
)), PASS_THROUGH);
$form = backdrop_get_form($node->type . '_node_form', $node);
return $form;
}
/**
* Implements hook_form_alter().
*/
function forward_revisions_form_alter(&$form, &$form_state, $form_id) {
$node_form_string = '_node_form';
if (substr_compare($form_id, $node_form_string, -strlen($node_form_string)) === 0) {
$node = $form['#node'];
$form['options']['#collapsed'] = TRUE;
$form['revision_information']['#collapsed'] = FALSE;
$form['options']['help_text'] = [
'#type' => 'markup',
'#weight' => -1,
'#markup' => '<strong>' . t('The publishing options will affect all revisions of this @node_type.', [
'@node_type' => node_type_get_name($node),
]) . '</strong>',
];
$args = arg();
if (!empty($args[4])) {
if ($args[4] == 'new_revision') {
$form['revision_information']['revision']['#type'] = 'hidden';
$form['revision_information']['revision']['#value'] = 1;
}
else if ($args[4] == 'edit_revision') {
$form['revision_information']['revision']['#type'] = 'hidden';
$form['revision_information']['revision']['#value'] = 0;
$form['options']['help_text'] = [
'#type' => 'markup',
'#weight' => -1,
'#markup' => '<strong>' . t('The publishing options will affect all versions of the node.') . '</strong>',
];
}
}
}
}
/**
* Submit handler for the new revision form.
*/
function forward_revisions_new_revision_submit($form, &$form_state) {
$form_state['values']['revision'] = 1;
}
/**
* Submit handler for the edit revision form.
*/
function forward_revisions_edit_revision_submit($form, &$form_state) {
$form_state['values']['revision'] = 0;
}
/**
* Implements hook_node_submit().
*/
function forward_revisions_node_submit(Node $node, $form, &$form_state) {
$args = arg();
if (!empty($args[4])) {
if ($args[4] == 'new_revision') {
$node->is_active_revision = FALSE;
}
}
// Update all the revisions to match the currently saved node.
db_update('node_revision')
->fields(['status' => $node->status])
->condition('nid', $node->nid)
->execute();
}