-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_comments.module
More file actions
109 lines (92 loc) · 3.42 KB
/
flat_comments.module
File metadata and controls
109 lines (92 loc) · 3.42 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
<?php
/**
* @file
* Contains flat_comments.module.
*/
use Drupal\comment\CommentInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function flat_comments_help($route_name, RouteMatchInterface $route_match) {
$output = '';
switch ($route_name) {
// Main module help for the flat_comments module.
case 'help.page.flat_comments':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Make all comments replies to the main post, regardless of the reply link used.') . '</p>';
$output;
default:
}
return $output;
}
/**
* Implements hook_form_alter().
*/
function flat_comments_form_field_config_edit_form_alter(
&$form,
FormStateInterface $form_state,
$form_id) {
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state->getFormObject()->getEntity();
// This only applies for comment fields.
if ($field->getType() != 'comment') {
return;
}
$form['actions']['submit']['#submit'][] = 'flat_comments_save';
// Add note about flat_comments to comment display description.
$form['settings']['default_mode']['#description'] .= t(' <strong>flat_comments enabled:</strong> Leave this box unchecked to force replies to be to the main post instead of other comments.');
// Add option to remove reply link from comments.
$form['settings']['remove_reply_link'] = [
'#type' => 'checkbox',
'#title' => t('Do not show a reply link on comments'),
'#default_value' => $field->getThirdPartySetting('flat_comments', 'remove_reply_link', FALSE),
];
}
function flat_comments_save(array &$form, FormStateInterface $form_state) {
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state->getFormObject()->getEntity();
$values = $form_state->getValues();
$field->setThirdPartySetting('flat_comments', 'remove_reply_link', $values['settings']['remove_reply_link']);
$field->save();
}
/**
* Implementation of hook_comment_presave().
*/
function flat_comments_comment_presave(CommentInterface $comment) {
$entity = \Drupal::entityManager()
->getStorage($comment->getCommentedEntityTypeId())
->load($comment->getCommentedEntityId());
$field_definition = \Drupal::entityManager()
->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
$field_definition = $field_definition[$comment->getFieldName()];
// Only affect new comments and comments set to be displayed flat.
if (!$comment->id() && $field_definition->getSetting('default_mode') === 0) {
// Set parent id to NULL to prevent threads.
$comment->get('pid')->setValue(NULL);
}
}
/**
* Implements hook_comments_links_alter().
*/
function flat_comments_comment_links_alter(
array &$links,
CommentInterface $entity,
array &$context) {
// If there is not a "reply" link, no need to continue.
if (!isset($links['comment']['#links']['comment-reply'])) {
return;
}
$comment = $entity;
$entity = \Drupal::entityManager()
->getStorage($comment->getCommentedEntityTypeId())
->load($comment->getCommentedEntityId());
$field_definition = \Drupal::entityManager()
->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
$field_definition = $field_definition[$comment->getFieldName()];
if($field_definition->getThirdPartySetting('flat_comments', 'remove_reply_link', FALSE)) {
unset($links['comment']['#links']['comment-reply']);
}
}