-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontent_moderation.module
More file actions
2467 lines (2220 loc) · 81 KB
/
content_moderation.module
File metadata and controls
2467 lines (2220 loc) · 81 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Content moderation.
*/
/**
* Implements hook_autoload_info().
*/
function content_moderation_autoload_info() {
return array(
'content_moderation_handler_field_history_link' => 'includes/content_moderation_handler_field_history_link.inc',
'content_moderation_handler_field_links' => 'includes/content_moderation_handler_field_links.inc',
'content_moderation_handler_field_state' => 'includes/content_moderation_handler_field_state.inc',
'content_moderation_handler_filter_state' => 'includes/content_moderation_handler_filter_state.inc',
'content_moderation_handler_filter_moderated_type' => 'includes/content_moderation_handler_filter_moderated_type.inc',
'content_moderation_handler_filter_user_can_moderate' => 'includes/content_moderation_handler_filter_user_can_moderate.inc',
// 'ContentModerationMigrateDestinationHandler' => 'content_moderation.migrate.inc',
);
}
/**
* Implements hook_config_info().
*/
function content_moderation_config_info() {
$prefixes['content_moderation.settings'] = array(
'label' => t('Content Moderation settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_menu().
*/
function content_moderation_menu() {
$items = array();
// Display a node's moderation history.
$items["node/%node/moderation"] = array(
'title' => 'Moderate',
'description' => 'Show the content moderation history.',
'type' => MENU_LOCAL_TASK,
'page callback' => 'content_moderation_node_history_view',
'page arguments' => array(1),
'access callback' => '_content_moderation_access',
'access arguments' => array('view history', 1),
'file' => 'content_moderation.node.inc',
);
// Unpublishing a live revision.
$items["node/%node/moderation/%/unpublish"] = array(
'title' => 'Unpublish revision',
'description' => 'Unpublish the current live revision.',
'type' => MENU_CALLBACK,
'page callback' => 'backdrop_get_form',
'page arguments' => array('content_moderation_node_unpublish_form', 1),
'load arguments' => array(3),
'access callback' => '_content_moderation_access',
'access arguments' => array('unpublish', 1),
'file' => 'content_moderation.node.inc',
);
// Change the moderation state of a node.
// Used in content_moderation_get_moderation_links()
$items["node/%node/moderation/%/change-state/%"] = array(
'title' => 'Change moderation state',
'page callback' => 'content_moderation_moderate_callback',
'page arguments' => array(1, 5),
'load arguments' => array(3),
'access callback' => '_content_moderation_moderate_access',
'access arguments' => array(1, 5),
'type' => MENU_CALLBACK,
);
$items['node/%node/moderation/view'] = array(
'title' => 'Revisions',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
// View the current revision of a node. Redirects to node/%node if the current
// revision is published, and to node/%node/draft if the current revision is
// a draft.
$items["node/%node/current-revision"] = array(
'page callback' => 'content_moderation_node_current_view',
'page arguments' => array(1),
'access arguments' => array('access content'),
'file' => 'content_moderation.node.inc',
);
// View the current draft of a node.
$items["node/%node/draft"] = array(
'title' => 'View draft',
'page callback' => 'content_moderation_node_view_draft',
'page arguments' => array(1),
'access callback' => '_content_moderation_access_current_draft',
'access arguments' => array(1),
'file' => 'content_moderation.node.inc',
'type' => MENU_LOCAL_TASK,
'weight' => -9,
);
// Module settings.
$items["admin/config/content/moderation"] = array(
'title' => 'Content Moderation',
'description' => 'Configure content moderation.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('content_moderation_admin_states_form'),
'access arguments' => array('administer content moderation'),
'file' => 'content_moderation.admin.inc',
);
$items['admin/config/content/moderation/general'] = array(
'title' => 'States',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/content/moderation/transitions'] = array(
'title' => 'Transitions',
'type' => MENU_LOCAL_TASK,
'page callback' => 'backdrop_get_form',
'page arguments' => array('content_moderation_admin_transitions_form'),
'access arguments' => array('administer content moderation'),
'file' => 'content_moderation.admin.inc',
);
$items['admin/config/content/moderation/check-permissions'] = array(
'title' => 'Check permissions',
'type' => MENU_LOCAL_TASK,
'page callback' => 'backdrop_get_form',
'page arguments' => array('content_moderation_admin_check_role_form'),
'access arguments' => array('administer content moderation'),
'file' => 'content_moderation.admin.inc',
'weight' => 10,
);
// If the diff module is present, replicate its pages under the moderation
// tab.
if (module_exists('diff')) {
$diff_menu_items = diff_menu();
$items['node/%node/moderation/diff'] = array(
'type' => MENU_LOCAL_TASK,
'file path' => backdrop_get_path('module', 'diff'),
'title' => 'Compare revisions',
'page arguments' => array(1),
);
$items['node/%node/moderation/diff'] += $diff_menu_items['node/%node/revisions/list'];
$items['node/%node/moderation/diff/list'] = array(
'title' => 'Compare revisions',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['node/%node/moderation/diff/view'] = array(
'page arguments' => array(1, 5, 6),
'tab_parent' => 'node/%/moderation/diff/list',
'file path' => backdrop_get_path('module', 'diff'),
);
$items['node/%node/moderation/diff/view'] += $diff_menu_items['node/%node/revisions/view'];
}
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function content_moderation_form_diff_node_revisions_alter(&$form, &$form_state, $form_id) {
// If this form is appearing under moderation then add a submit function
// that will keep the user in the moderation tab.
if (arg(2) == 'moderation') {
$form['#submit'][] = 'content_moderation_diff_node_revisions_submit';
}
}
/**
* Redirects the the diff_node_revisions form when the user is under the
* moderation tab.
*/
function content_moderation_diff_node_revisions_submit($form, &$form_state) {
// The ids are ordered so the old revision is always on the left.
$old_vid = min($form_state['values']['old'], $form_state['values']['new']);
$new_vid = max($form_state['values']['old'], $form_state['values']['new']);
$form_state['redirect'] = 'node/'. $form_state['values']['nid'] .'/moderation/diff/view/'. $old_vid .'/'. $new_vid;
}
/**
* Implements hook_menu_alter().
*/
function content_moderation_menu_alter(&$items) {
// Hijack the node/X/edit page to ensure that the right revision (most
// current) is displayed.
$items['node/%node/edit']['page callback'] = 'content_moderation_node_edit_page_override';
// Override the node edit menu item title.
$items['node/%node/edit']['title callback'] = 'content_moderation_edit_tab_title';
$items['node/%node/edit']['title arguments'] = array(1);
// Override the node view menu item title.
$items['node/%node/view']['title callback'] = 'content_moderation_view_tab_title';
$items['node/%node/view']['title arguments'] = array(1);
// Redirect node/%node/revisions.
$items['node/%node/revisions']['page callback'] = 'content_moderation_node_revisions_redirect';
$items['node/%node/revisions']['page arguments'] = array(1);
// Override the node revision view callback.
$items['node/%node/revisions/%/view']['page callback'] = 'content_moderation_node_view_revision';
$items['node/%node/revisions/%/view']['file path'] = backdrop_get_path('module', 'content_moderation');
$items['node/%node/revisions/%/view']['file'] = 'content_moderation.node.inc';
// For revert and delete operations, use our own access check.
$items['node/%node/revisions/%/revert']['access callback'] = '_content_moderation_revision_access';
$items['node/%node/revisions/%/revert']['access arguments'] = array(
1,
'update',
);
$items['node/%node/revisions/%/delete']['access callback'] = '_content_moderation_revision_access';
$items['node/%node/revisions/%/delete']['access arguments'] = array(
1,
'delete',
);
// Provide a container administration menu item, if one doesn't already exist.
if (!isset($items['admin/config/content'])) {
$items['admin/config/content'] = array(
'title' => 'Content Moderation',
'description' => 'Content moderation',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'position' => 'right',
'file' => 'system.admin.inc',
'file path' => backdrop_get_path('module', 'system'),
);
}
}
/**
* Redirects 'node/%node/revisions' to node/%node/moderation
*
* content_moderation_menu_alter() changes the page callback
* for 'node/%node/revisions' to this function.
*
* @param Node $node
* The node being acted upon.
*/
function content_moderation_node_revisions_redirect(Node $node) {
// Redirect nodes subject to moderation.
if (content_moderation_node_moderated($node) === TRUE) {
backdrop_goto('node/' . $node->nid . '/moderation');
}
// Return the normal node revisions page for unmoderated types.
else {
if (module_exists('diff')) {
return diff_diffs_overview($node);
}
else {
return node_revision_overview($node);
}
}
}
/**
* Implements hook_menu_local_tasks_alter().
*
* Hide the node revisions tab conditionally.
*
* Check if the node type is subject to moderation. If so, unset the revision
* tab. This step is necessary because hook_menu_alter cannot change the menu
* item type on a node type by node type basis for node/%node/revision.
*
* Additionally, content_menu_alter() is used to change the page callback
* for node/%node/revisions so that this URL redirects to node/%node/moderation
* for node types subject to moderation.
*/
function content_moderation_menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Do we need to bother doing anything?
if (empty($data['tabs'][0]['output'])) {
return;
}
// Check the path.
$arg = arg(0, $root_path);
$arg1 = arg(1, $root_path);
if ($arg != 'node' || $arg1 != '%') {
return;
}
// Get the node for the current menu router.
if ($node = menu_get_object()) {
// Here is the reason this hook implementation exists:
// If this is a node that gets moderated, don't show 'node/%/revisions'.
if (content_moderation_node_moderated($node) === TRUE) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if (!empty($value['#link']['path']) && $value['#link']['path'] == 'node/%/revisions') {
unset($data['tabs'][0]['output'][$key]);
$data['tabs'][0]['count'] -= 1;
break;
}
}
}
}
}
/**
* Change the name of the node edit tab, conditionally.
*
* - Don't change the title if the content is not under moderation.
*
* - If a piece of content has a published revision and the published revision
* is also the current moderation revision, the "Edit" tab should be titled
* "Create draft".
*
* - If a piece of content has a published revision and the current moderation
* revision is a newer, or if the content has no published revision, the
* "Edit" tab should be titled "Edit draft".
*
* @param Node $node
* The node being acted upon.
*
* @return string
* The title for the tab.
*/
function content_moderation_edit_tab_title(Node $node) {
// Use the normal tab title if the node is not under moderation.
if (!content_moderation_node_moderated($node)) {
return t('Edit');
}
// Is the latest draft published?
$state = $node->content_moderation;
if (!empty($state['published']) && $state['published']->vid == $state['current']->vid) {
return t('New draft');
}
// The latest draft is not published.
return t('Edit draft');
}
/**
* Change the name of the node view tab, conditionally.
*
* - Don't change the title if the content is not under moderation.
*
* - If a piece of content has a published revision, the "View" tab should be
* titled "View published".
*
* - Otherwise, it should be titled "View draft".
*
* @param Node $node
* The node being acted upon.
*
* @return string
* The title for the tab.
*/
function content_moderation_view_tab_title(Node $node) {
// Use the normal tab title if the node is not under moderation.
if (!content_moderation_node_moderated($node)) {
return t('View');
}
// Is there a published revision?
$state = $node->content_moderation;
if (!empty($state['published'])) {
return t('View published');
}
return t('View draft');
}
/**
* Implements hook_admin_paths().
*/
function content_moderation_admin_paths() {
if (config_get('system.core', 'admin_theme')) {
$paths = array(
'node/*/moderation' => TRUE,
'node/*/moderation/*/unpublish' => TRUE,
'node/*/moderation/*/change-state/*' => TRUE,
'node/*/moderation/view' => TRUE,
'node/*/moderation/diff' => TRUE,
'node/*/moderation/diff/list' => TRUE,
'node/*/moderation/diff/view' => TRUE,
'node/*/moderation/diff/view/*/*' => TRUE,
);
return $paths;
}
}
/**
* Implements hook_theme().
*/
function content_moderation_theme() {
return array(
'content_moderation_admin_states_form' => array(
'file' => 'content_moderation.admin.inc',
'render element' => 'form',
),
'content_moderation_admin_transitions_form' => array(
'file' => 'content_moderation.admin.inc',
'render element' => 'form',
),
);
}
/**
* Implements hook_permission().
*
* Provides permissions for each state to state change.
*/
function content_moderation_permission() {
$permissions = array();
$permissions['view all unpublished content'] = array(
'title' => t('View all unpublished content'),
);
$permissions['administer content moderation'] = array(
'title' => t('Administer Content Moderation'),
);
$permissions['bypass content moderation'] = array(
'title' => t('Bypass moderation restrictions'),
'restrict access' => TRUE,
);
$permissions['view moderation history'] = array(
'title' => t('View moderation history'),
);
$permissions['view moderation messages'] = array(
'title' => t('View the moderation messages on a node'),
);
$permissions['use content_moderation my drafts tab'] = array(
'title' => t('Use "My drafts" content tab'),
);
$permissions['use content_moderation needs review tab'] = array(
'title' => t('Use "Needs review" content tab'),
);
// Per-node-type, per-transition permissions. Used by
// content_moderation_state_allowed().
$node_types = content_moderation_moderate_node_types();
$transitions = content_moderation_transitions();
foreach ($transitions as $transition) {
$from_state = $transition['from_name'];
$to_state = $transition['to_name'];
// Always set a permission to perform all moderation states.
$permissions["moderate content from $from_state to $to_state"] = array(
'title' => t('Moderate all content from %from_state to %to_state', array(
'%from_state' => content_moderation_state_label($from_state),
'%to_state' => content_moderation_state_label($to_state),
)),
);
// Per-node type permissions are very complex, and should only be used if
// absolutely needed. For right now, this is hardcoded to OFF. To enable it,
// Add this line to settings.php and then reset permissions:
// $settings['content_moderation_per_node_type'] = TRUE; // Permissions.
if (settings_get('content_moderation_per_node_type', FALSE)) {
foreach ($node_types as $node_type) {
$permissions["moderate $node_type state from $from_state to $to_state"] = array(
'title' => t('Moderate %node_type state from %from_state to %to_state', array(
'%node_type' => node_type_get_name($node_type),
'%from_state' => content_moderation_state_label($from_state),
'%to_state' => content_moderation_state_label($to_state),
)),
);
}
}
}
return $permissions;
}
/**
* Implements hook_node_access().
*
* Allows users with the 'view all unpublished content' permission to do so.
*/
function content_moderation_node_access($node, $op, $account) {
if ($op == 'view' && !$node->status && user_access('view all unpublished content', $account)) {
return NODE_ACCESS_ALLOW;
}
return NODE_ACCESS_IGNORE;
}
/**
* Custom access handler for node operations.
*
* @param string $op
* The operation being requested.
* @param Node $node
* The node being acted upon.
*
* @return bool
* TRUE or FALSE.
*/
function _content_moderation_access($op, Node $node) {
global $user;
// If we do not control this node type, deny access.
if (content_moderation_node_type_moderated($node->type) === FALSE) {
return FALSE;
}
$access = TRUE;
// The user must be able to view the moderation history.
$access &= user_access('view moderation history');
// The user must be able to edit this node.
$access &= node_access('update', $node);
if ($op == 'unpublish') {
// content_moderation_states_next() checks transition permissions.
$next_states = content_moderation_states_next(content_moderation_state_published(), $user, $node);
$access &= !empty($next_states);
}
// Allow other modules to change our rule set.
backdrop_alter('content_moderation_access', $access, $op, $node);
return $access;
}
/**
* Wrapper for the 'revert' and 'delete' operations of _node_revision_access().
*
* Backdrop core's "current revision" of a node is the version in {node}; for
* Content Moderation, latest revision in {node_revision} is the current
* revision. For nodes with a published revision, Content Moderation keeps
* that revision in {node}, whether or not it is the current revision.
*
* @param Node $node
* The node being acted on.
* @param string $op
* The operation being requested.
*/
function _content_moderation_revision_access(Node $node, $op) {
// Normal behavior for unmoderated nodes.
if (!content_moderation_node_moderated($node)) {
return _node_revision_access($node, $op);
}
// Prevent reverting to (ie, update) the current revision.
if ($node->content_moderation['current']->vid == $node->content_moderation['my_revision']->vid) {
if ($op == 'update') {
return FALSE;
}
}
// Prevent deleting the current revision, if there is no separate published
// revision. This also prevents deleting the current revision if it is the
// only revision, and its unpublished.
if ($node->content_moderation['current']->vid == $node->content_moderation['my_revision']->vid) {
if ($op == 'delete' && !isset($node->content_moderation['published'])) {
// In theory, deleting the one and only revision of a node could be
// allowed but we'd need to add special logic that actually deletes
// the node, not just the revision.
return FALSE;
}
}
// Prevent deleting a published revision.
if (isset($node->content_moderation['published']) && $node->content_moderation['published']->vid == $node->content_moderation['my_revision']->vid) {
if ($op == 'delete') {
// In theory, deleting a published revision could be allowed but we'd
// need to solve the problem of determining what to do if you delete the
// published revision, e.g., what database tables and fields would need
// to be cascaded for such a change.
return FALSE;
}
}
// Check access.
return _node_revision_access($node, $op);
}
/**
* Checks if a user may make a particular transition on a node.
*
* @param Node $node
* The node being acted upon.
* @param string $state
* The new moderation state.
*
* @return bool
* Booelan TRUE or FALSE.
*/
function _content_moderation_moderate_access(Node $node, $state) {
global $user;
$my_revision = $node->content_moderation['my_revision'];
$next_states = content_moderation_states_next($my_revision->state, $user, $node);
// Determining node access:
// - The user can edit the node.
// - This is the current revision (no branching the revision history).
// - There are next states the user may transition to.
// - This state is in the available next states.
$access = node_access('update', $node, $user)
&& $my_revision->is_current
&& (!empty($next_states))
&& isset($next_states[$state]);
// Allow other modules to change our rule set.
$op = 'moderate';
backdrop_alter('content_moderation_access', $access, $op, $node);
return $access;
}
/**
* Checks if the user can view the current node revision.
*
* This is the access callback for node/%node/draft as defined in
* content_moderation_menu().
*
* @param Node $node
* The node being acted upon.
*
* @return bool
* TRUE or FALSE.
*/
function _content_moderation_access_current_draft(Node $node) {
// This tab should only appear for nodes under moderation.
if (!content_moderation_node_moderated($node)) {
return FALSE;
}
$state = $node->content_moderation;
return (_content_moderation_access('view revisions', $node)
&& !empty($state['published'])
&& $state['published']->vid != $state['current']->vid);
}
/**
* Implements hook_help().
*/
function content_moderation_help($path, $arg) {
switch ($path) {
case 'admin/help#content_moderation':
return '<p>' . t("Enables you to control node display with a moderation workflow. You can have a 'Live revision' for all visitors and pending revisions which need to be approved to become the new 'Live revision.'") . '</p>';
case 'admin/config/content/moderation':
return '<p>' . t("These are the states through which a node passes in order to become published. By default, the Content Moderation module provides the states 'Draft,' 'Needs review,' and 'Published'. On this screen you may create, delete and re-order states. Additional states might include 'Legal review,' 'PR review', or any or state your site may need.") . '</p>';
case 'admin/config/content/moderation/transitions':
return '<p>' . t('The Content Moderation module keeps track of when a node moves from one state to another. By default, nodes begin in the %draft state and end in the %published state. The transitions on this page control how nodes move from state to state. <a href="@permissions">Permission to perform these transitions is controlled on a role-by-role basis</a>.',
array(
'%draft' => content_moderation_state_label(content_moderation_state_none()),
'%published' => content_moderation_state_label(content_moderation_state_published()),
'@permissions' => url('admin/people/permissions',
array(
'fragment' => 'module-content_moderation',
),
),
)
) . '</p>';
case 'admin/config/content/moderation/check-permissions':
return '<p>' . t("In order to participate in the moderation process, Backdrop users must be granted several node- and moderation- related permissions. This page can help check whether roles have the correct permissions to author, edit, moderate, and publish moderated content.") . '</p>';
}
}
/**
* Implements hook_views_api().
*/
function content_moderation_views_api() {
return array('api' => 2.0);
}
/**
* Implements hook_entity_presave().
*/
function content_moderation_entity_presave($entity, $entity_type) {
// Note: this only supports nodes at the moment.
if ($entity_type != 'node') {
return;
}
// Is the content type under moderation?
if (!content_moderation_node_type_moderated($entity->type)) {
return;
}
// Load our data onto the entity.
if (!$entity->isNew()) {
content_moderation_set_node_state($entity);
}
$published = FALSE;
// Ensure the entity is marked as published.
if (isset($entity->content_moderation_state_new) && $entity->content_moderation_state_new == content_moderation_state_published()) {
$published = TRUE;
$entity->status = NODE_PUBLISHED;
}
// If we are unpublishing a node, do not force revision.
if (!$entity->status) {
// @todo do we have to set it as the active revision or not?
$entity->is_draft_revision = FALSE;
}
// Inform drafty if this is a new revision, if it has not already been
// marked as such.
elseif (!isset($entity->is_draft_revision) && !$published) {
$entity->is_draft_revision = TRUE;
}
}
/**
* Implements hook_node_insert().
*
* Store moderation data for this node.
*/
function content_moderation_node_insert($node) {
content_moderation_save($node);
}
/**
* Loads moderation information onto a node being saved.
*
* @param Node $node
* The node being acted on.
*/
function content_moderation_set_node_state(Node $node) {
global $user;
// Set moderation state values.
if (!isset($node->content_moderation_state_current)) {
$node->content_moderation_state_current = !empty($node->original->content_moderation['current']->state) ? $node->original->content_moderation['current']->state : content_moderation_state_none();
}
if (!isset($node->content_moderation_state_new)) {
$default_state = config_get('node.type.' . $node->type, 'settings.content_moderation_default_state');
// Moderating the default revision. Capture the proper state from drafty.
if (!empty($node->default_revision) && $node->status) {
$node->content_moderation_state_new = content_moderation_state_published();
}
// Moving from published to unpublished.
elseif ($node->status == NODE_NOT_PUBLISHED && isset($node->original->status) && $node->original->status == NODE_PUBLISHED) {
// @todo Currently we cannot set the state correctly if the default state
// is "Published".
// @see https://www.drupal.org/node/1436260
$node->content_moderation_state_new = ($default_state == NULL) ? content_moderation_state_none() : $default_state;
}
// Moving from unpublished to published.
elseif ($node->status == NODE_PUBLISHED && isset($node->original->status) && $node->original->status == NODE_NOT_PUBLISHED) {
$node->content_moderation_state_new = content_moderation_state_published();
}
else {
if (!empty($node->original->content_moderation['current']->state)) {
$node->content_moderation_state_new = $node->original->content_moderation['current']->state;
}
else {
$node->content_moderation_state_new = ($default_state == NULL) ? content_moderation_state_none() : $default_state;
}
}
}
// If this is a new node, give it some information about 'my revision'.
if (!isset($node->content_moderation)) {
$node->content_moderation = array();
$node->content_moderation['my_revision'] = $node->content_moderation['current'] = (object) array(
'from_state' => content_moderation_state_none(),
'state' => content_moderation_state_none(),
'nid' => $node->nid,
'vid' => $node->vid,
'uid' => $user->uid,
'is_current' => TRUE,
'published' => FALSE,
'stamp' => $node->changed,
);
}
}
/**
* Implements hook_node_update().
*
* Stores the moderation information for this node.
*/
function content_moderation_node_update($node) {
content_moderation_save($node);
}
/**
* Saves the moderation history for the node.
*
* @param Node $node
* The node being acted on.
*/
function content_moderation_save(Node $node) {
$current_draft = &backdrop_static(__FUNCTION__, 0);
global $user;
// Don't proceed if moderation is not enabled on this content or if we
// are saving the published version from drafty.
if (!content_moderation_node_moderated($node)) {
return;
}
// Ensure that we have loaded our data onto the node. This function will
// check that the required properties are set for all nodes.
content_moderation_set_node_state($node);
// Prepare the state information.
$state = $node->content_moderation_state_new;
$old_revision = $node->content_moderation['my_revision'];
$node->is_current = FALSE;
if (empty($node->default_revision)) {
$node->is_current = TRUE;
}
// Build a history record.
$new_revision = (object) array(
'from_state' => $old_revision->state,
'state' => $state,
'nid' => $node->nid,
'vid' => $node->vid,
'uid' => $user->uid,
'is_current' => !empty($node->is_current),
'published' => ($state == content_moderation_state_published()),
'stamp' => $_SERVER['REQUEST_TIME'],
);
// If this is the new 'current' moderation record, it should be the only one
// flagged 'current' in {content_moderation_node_history}.
if ($new_revision->is_current) {
$query = db_update('content_moderation_node_history')
->condition('nid', $node->nid)
->fields(array('is_current' => 0))
->execute();
}
// If this revision is to be published, the new moderation record should be
// the only one flagged 'published' in {content_moderation_node_history}.
// Also applies in the case where we unpublish a live revision.
if ($new_revision->published || !$node->status) {
$query = db_update('content_moderation_node_history')
->condition('nid', $node->nid)
->fields(array('published' => 0))
->execute();
}
// Save the node history record.
backdrop_write_record('content_moderation_node_history', $new_revision);
// On a moderation loop, inform other modules of the change.
if (!empty($node->is_current)) {
// Clear the node's cache.
entity_get_controller('node')->resetCache(array($node->nid));
// Notify other modules that the state was changed.
module_invoke_all('content_moderation_transition', $node, $node->content_moderation['my_revision']->state, $node->content_moderation_state_new);
}
}
/**
* Implements hook_node_load().
*
* Load moderation history and status on a node.
*/
function content_moderation_node_load($nodes, $types) {
foreach ($nodes as $node) {
// Add the node history.
content_moderation_node_data($node);
}
}
/**
* Implements hook_node_view().
*
* Display messages about the node's moderation state.
*/
function content_moderation_node_view($node, $view_mode = 'full') {
// Show moderation state messages if we're on a node page.
if (node_is_page($node) && $view_mode == 'full' && empty($node->in_preview)) {
content_moderation_messages('view', $node);
}
}
/**
* Implements hook_node_delete().
*/
function content_moderation_node_delete($node) {
// Delete node history when it is deleted.
db_delete('content_moderation_node_history')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Add moderation rules to node types.
*/
function content_moderation_form_node_type_form_alter(&$form, $form_state) {
$node_type = $form['#node_type'];
// Get a list of moderation states.
$options = content_moderation_state_labels();
// Disable the 'revision' checkbox when the 'moderation' checkbox is checked,
// so that moderation can not be enabled unless revisions are enabled.
$form['revision']['revision']['revision_enabled']['#states'] = array(
'disabled' => array(':input[name="moderation_enabled"]' => array('checked' => TRUE)),
);
$form['content_moderation'] = array(
'#type' => 'fieldset',
'#title' => t('Moderation'),
'#group' => 'additional_settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('administer content moderation'),
'#attached' => array(
'js' => array(backdrop_get_path('module', 'content_moderation') . '/js/content_moderation.js'),
),
);
// Disable the 'moderation' checkbox when the 'revision' checkbox is not
// checked, so that revisions can not be turned off without also turning off
// moderation.
$form['content_moderation']['moderation_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable moderation of revisions'),
'#default_value' => !isset($node_type->settings['moderation_enabled']) ? 0 : $node_type->settings['moderation_enabled'],
'#description' => t('Revisions must be enabled in order to use moderation.'),
'#states' => array(
'disabled' => array(
':input[name="revision_enabled"]' => array(
'checked' => FALSE,
),
),
),
);
// This select element is hidden when moderation is not enabled.
$form['content_moderation']['content_moderation_default_state'] = array(
'#title' => t('Default moderation state'),
'#type' => 'select',
'#options' => $options,
'#default_value' => !isset($node_type->settings['content_moderation_default_state']) ? NULL : $node_type->settings['content_moderation_default_state'],
'#description' => t('Set the default moderation state for this content type. Users with additional moderation permissions will be able to set the moderation state when creating or editing nodes.'),
'#states' => array(
'visible' => array(':input[name="moderation_enabled"]' => array('checked' => TRUE)),
),
);
$form['#validate'][] = 'content_moderation_node_type_form_validate';
$form['#submit'][] = 'content_moderation_node_type_form_submit';
}
/**
* Validate the content type form.
*/
function content_moderation_node_type_form_validate($form, &$form_state) {
// Ensure that revisions are enabled if moderation is.
if ($form_state['values']['moderation_enabled']) {
$form_state['values']['status_default'] = 0;
$form_state['values']['revision_enabled'] = 1;
}
}
/**
* Submit handler for node type form.
*/
function content_moderation_node_type_form_submit($form, &$form_state) {
if (isset($form_state['values']['moderation_enabled'])) {
$config = config('node.type.' . $form_state['values']['type']);
$config->set('settings.moderation_enabled', $form_state['values']['moderation_enabled']);
$config->set('settings.content_moderation_default_state', $form_state['values']['content_moderation_default_state']);
$config->save();
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Forcing new reversion and publishing.
*/
function content_moderation_form_node_form_alter(&$form, $form_state) {
global $user;
// This must be a node form and a node that has moderation enabled.
// Extended to include moderation check on the individual node.
if (!content_moderation_node_moderated($form['#node'])) {
return;
}
// Set a moderation state even if there is not one defined.
if (isset($form['#node']->content_moderation['current']->state)) {
$moderation_state = $form['#node']->content_moderation['current']->state;
$current_label = t('@state (Current)', array('@state' => content_moderation_state_label($moderation_state)));
}
else {
$moderation_state = content_moderation_state_none();
$current_label = content_moderation_state_label($moderation_state);
}