-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment_approver.module
61 lines (54 loc) · 1.59 KB
/
comment_approver.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
<?php
/**
* @file
* Contains comment_approver.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\comment_approver\CommentTesterInterface;
/**
* Implements hook_help().
*/
function comment_approver_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the comment_approver module.
case 'help.page.comment_approver':
$output = '<pre>' . file_get_contents(drupal_get_path('module', 'comment_approver') . '/README.md') . '</pre>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function comment_approver_theme() {
return [
'comment_approver' => [
'render element' => 'children',
],
];
}
/**
* Implements hook_Entity_type_presave().
*/
function comment_approver_comment_presave(EntityInterface $entity) {
$comment = $entity;
$commentTester = \Drupal::service('comment_approver.tester');
$current_mode = $commentTester->getMode();
if (!$entity->get('status')->value) {
if ($current_mode == CommentTesterInterface::APPROVER) {
// Perform approval test
// Test for automated approval of comment if skip comment approval is
// disabled and set the comment status to 1 if test passes.
if ($commentTester->test($comment)) {
$entity->set('status', 1);
}
}
}
elseif (!Drupal::currentUser()->hasPermission('administer comments') && $current_mode == CommentTesterInterface::BLOCKER) {
// Perform blocker test.
if (!$commentTester->test($comment)) {
$entity->set('status', 0);
}
}
}