-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomment_limit.module
More file actions
241 lines (218 loc) · 8.33 KB
/
comment_limit.module
File metadata and controls
241 lines (218 loc) · 8.33 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
<?php
/**
* @file
* Contains comment_limit.module..
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\field\Entity\FieldConfig;
/**
* Implements hook_help().
*/
function comment_limit_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the comment_limit module.
case 'help.page.comment_limit':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Limits comments per field or user') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function comment_limit_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
$comment = $form_state->getFormObject()->getEntity();
$field_type = $comment->getFieldStorageDefinition()->getType();
if ($field_type == 'comment') {
$form['comment_limit'] = [
'#type' => 'fieldset',
'#title' => 'Comment limit',
];
$default_user = $comment->getThirdPartySetting('comment_limit', 'user_limit', 0);
$form['comment_limit']['user_limit'] = [
'#type' => 'number',
'#title' => 'User limit',
'#description' => t('The maximum number of comments per user on this entity, e.g. 2 or 0 for no limit'),
'#weight' => 1,
'#min' => '0',
'#default_value' => $default_user,
];
$default_entity = $comment->getThirdPartySetting('comment_limit', 'field_limit', 0);
$form['comment_limit']['field_limit'] = [
'#type' => 'number',
'#title' => 'Field limit',
'#description' => t('The maximum number of comments for this field, e.g. 10 or 0 for no limit'),
'#weight' => 1,
'#min' => '0',
'#default_value' => $default_entity,
];
$form['#entity_builders'][] = 'comment_limit_form_field_edit_form_add_form_builder';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function comment_limit_form_comment_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$comment_limit = Drupal::service('comment_limit.service');
$comment = $form_state->getFormObject()->getEntity();
// PHP notice fix undefined variable.
$field_id = NULL;
$form_action_parts = explode('/', $form['#action']);
$field_name = $form_action_parts[count($form_action_parts) - 1];
$entity = Drupal::entityTypeManager()
->getStorage($comment->getCommentedEntityTypeId())
->load($comment->getCommentedEntityId());
$field_config = FieldConfig::loadByName(
$comment->getCommentedEntityTypeId(),
$entity->bundle(),
$field_name
);
if ($field_config instanceof FieldConfig) {
$field_id = $field_config->id();
$field_label = $field_config->getLabel();
}
$definitions = DataDefinition::create('any')
->addConstraint('CommentFormConstraint', [
'entityId' => $comment->getCommentedEntityId(),
'entityType' => $comment->getCommentedEntityTypeId(),
'fieldId' => $field_id,
'fieldName' => $field_name,
]);
$typed_data = Drupal::typedDataManager()->create($definitions);
$violations = $typed_data->validate();
if ($violations->count() > 0) {
// Check access of the form elements.
$access = \Drupal::currentUser()->hasPermission('bypass comment limit');
foreach ($form as $key => $value) {
if (strpos($key, '#') === FALSE) {
$form[$key]['#access'] = $access;
}
}
// Add information about the limit.
$form['comment_limit_' . $field_name] = [
'#type' => 'html_tag',
'#tag' => 'em',
'#value' => $comment_limit->getMessage(
$comment->getCommentedEntityId(),
$comment->getCommentedEntityTypeId(),
$field_name,
$field_id,
$field_label
),
'#access' => Drupal::currentUser()->hasPermission('post comments'),
];
}
}
/**
* Custom formbuilder for field config form.
*
* @param string $entity_type
* The entity type.
* @param FieldConfig $comment
* The FieldConfig object of the comment entity.
* @param FormStateInterface $form_state
* The form values from the field config form.
*/
function comment_limit_form_field_edit_form_add_form_builder($entity_type, FieldConfig $comment, &$form, FormStateInterface $form_state) {
if ($form_state->getValue('user_limit') == NULL) {
$form_state->setValue('user_limit', '0');
}
else {
$comment->setThirdPartySetting('comment_limit', 'user_limit', $form_state->getValue('user_limit'));
}
if ($form_state->getValue('field_limit') == NULL) {
$form_state->setValue('field_limit', '0');
}
else {
$comment->setThirdPartySetting('comment_limit', 'field_limit', $form_state->getValue('field_limit'));
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function comment_limit_entity_extra_field_info() {
$extra = [];
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundles_info */
$bundles_info = Drupal::service('entity_type.bundle.info');
$bundles = $bundles_info->getAllBundleInfo();
/** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
$entity_field_manager = \Drupal::service('entity_field.manager');
/** @var \Drupal\comment_limit\CommentLimit $comment_limit */
$comment_limit = Drupal::service('comment_limit.service');
$entity_types = $comment_limit->getAllEntityTypes();
foreach ($entity_types as $entity_type_id) {
foreach ($bundles[$entity_type_id] as $bundle_name => $bundle) {
// Get all the bundles field definitions.
$field_configs = $entity_field_manager->getFieldDefinitions($entity_type_id, $bundle_name);
foreach ($field_configs as $field_name => $field_config) {
// Check if we have a comment field.
if ($field_config->getType() === 'comment') {
$extra[$entity_type_id][$bundle_name]['display']['field_limit_' . $field_name] = [
'label' => t('Field limit status for @field_name', ['@field_name' => $field_name]),
'description' => t('Shows the limit status of the field'),
'weight' => 0,
'visible' => FALSE,
];
$extra[$entity_type_id][$bundle_name]['display']['user_limit_' . $field_name] = [
'label' => t('User limit status for @field_name', ['@field_name' => $field_name]),
'description' => t('Shows the limit status of the user'),
'weight' => 0,
'visible' => FALSE,
];
}
}
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*/
function comment_limit_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
$fields = $entity->getFieldDefinitions();
foreach ($fields as $delta => $field) {
if ($field->getType() == 'comment') {
$comment_limit = Drupal::service('comment_limit.service');
$field_limit = $comment_limit->getFieldLimit($field->id());
$user_limit = $comment_limit->getUserLimit($field->id());
$field_current_count = $comment_limit->getCurrentCommentsOnField(
$entity->id(),
$entity->getEntityTypeId(),
$field->getName()
);
$user_current_count = $comment_limit->getCurrentCommentCountForUser(
$entity->id(),
$entity->getEntityTypeId(),
$field->getName()
);
if ($component = $display->getComponent('field_limit_' . $field->getName())) {
$build['field_limit_' . $field->getName()] = [
'#type' => 'item',
'#weight' => $component['weight'],
'#markup' => '<em>' . t('The comment field "%field" is generally limited to a maximum of %maximum comments, %count have already been posted.', [
'%field' => $field->getLabel(),
'%maximum' => $field_limit,
'%count' => $field_current_count,
]) . '</em>',
];
}
if ($component = $display->getComponent('user_limit_' . $field->getName())) {
$build['user_limit_' . $field->getName()] = [
'#type' => 'item',
'#weight' => $component['weight'],
'#markup' => '<em>' . t('You may post a maximum of %maximum comments for the comment field "%field", having %count already posted', [
'%field' => $field->getLabel(),
'%maximum' => $user_limit,
'%count' => $user_current_count,
]) . '</em>',
];
}
}
}
}