Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\comment;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
6 use Drupal\Component\Datetime\TimeInterface;
|
Chris@0
|
7 use Drupal\Component\Utility\Html;
|
Chris@0
|
8 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
9 use Drupal\Core\Datetime\DrupalDateTime;
|
Chris@0
|
10 use Drupal\Core\Entity\ContentEntityForm;
|
Chris@0
|
11 use Drupal\Core\Entity\EntityConstraintViolationListInterface;
|
Chris@17
|
12 use Drupal\Core\Entity\EntityFieldManagerInterface;
|
Chris@17
|
13 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@0
|
14 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
Chris@0
|
15 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
16 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
17 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Base handler for comment forms.
|
Chris@14
|
22 *
|
Chris@14
|
23 * @internal
|
Chris@0
|
24 */
|
Chris@0
|
25 class CommentForm extends ContentEntityForm {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The current user.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $currentUser;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The renderer.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $renderer;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@17
|
42 * The entity field manager.
|
Chris@17
|
43 *
|
Chris@17
|
44 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
|
Chris@17
|
45 */
|
Chris@17
|
46 protected $entityFieldManager;
|
Chris@17
|
47
|
Chris@17
|
48 /**
|
Chris@0
|
49 * {@inheritdoc}
|
Chris@0
|
50 */
|
Chris@0
|
51 public static function create(ContainerInterface $container) {
|
Chris@0
|
52 return new static(
|
Chris@17
|
53 $container->get('entity.repository'),
|
Chris@0
|
54 $container->get('current_user'),
|
Chris@0
|
55 $container->get('renderer'),
|
Chris@0
|
56 $container->get('entity_type.bundle.info'),
|
Chris@17
|
57 $container->get('datetime.time'),
|
Chris@17
|
58 $container->get('entity_field.manager')
|
Chris@0
|
59 );
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Constructs a new CommentForm.
|
Chris@0
|
64 *
|
Chris@17
|
65 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@17
|
66 * The entity repository.
|
Chris@0
|
67 * @param \Drupal\Core\Session\AccountInterface $current_user
|
Chris@0
|
68 * The current user.
|
Chris@0
|
69 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
70 * The renderer.
|
Chris@0
|
71 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
Chris@0
|
72 * The entity type bundle service.
|
Chris@0
|
73 * @param \Drupal\Component\Datetime\TimeInterface $time
|
Chris@0
|
74 * The time service.
|
Chris@0
|
75 */
|
Chris@17
|
76 public function __construct(EntityRepositoryInterface $entity_repository, AccountInterface $current_user, RendererInterface $renderer, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, EntityFieldManagerInterface $entity_field_manager = NULL) {
|
Chris@17
|
77 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
|
Chris@0
|
78 $this->currentUser = $current_user;
|
Chris@0
|
79 $this->renderer = $renderer;
|
Chris@17
|
80 $this->entityFieldManager = $entity_field_manager ?: \Drupal::service('entity_field.manager');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * {@inheritdoc}
|
Chris@0
|
85 */
|
Chris@0
|
86 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
87 /** @var \Drupal\comment\CommentInterface $comment */
|
Chris@0
|
88 $comment = $this->entity;
|
Chris@17
|
89 $entity = $this->entityTypeManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
|
Chris@0
|
90 $field_name = $comment->getFieldName();
|
Chris@17
|
91 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
|
Chris@0
|
92 $config = $this->config('user.settings');
|
Chris@0
|
93
|
Chris@0
|
94 // In several places within this function, we vary $form on:
|
Chris@0
|
95 // - The current user's permissions.
|
Chris@0
|
96 // - Whether the current user is authenticated or anonymous.
|
Chris@0
|
97 // - The 'user.settings' configuration.
|
Chris@0
|
98 // - The comment field's definition.
|
Chris@0
|
99 $form['#cache']['contexts'][] = 'user.permissions';
|
Chris@0
|
100 $form['#cache']['contexts'][] = 'user.roles:authenticated';
|
Chris@0
|
101 $this->renderer->addCacheableDependency($form, $config);
|
Chris@0
|
102 $this->renderer->addCacheableDependency($form, $field_definition->getConfig($entity->bundle()));
|
Chris@0
|
103
|
Chris@0
|
104 // Use #comment-form as unique jump target, regardless of entity type.
|
Chris@0
|
105 $form['#id'] = Html::getUniqueId('comment_form');
|
Chris@0
|
106 $form['#theme'] = ['comment_form__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, 'comment_form'];
|
Chris@0
|
107
|
Chris@0
|
108 $anonymous_contact = $field_definition->getSetting('anonymous');
|
Chris@0
|
109 $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments');
|
Chris@0
|
110
|
Chris@18
|
111 if (!$this->currentUser->isAuthenticated() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT) {
|
Chris@0
|
112 $form['#attached']['library'][] = 'core/drupal.form';
|
Chris@0
|
113 $form['#attributes']['data-user-info-from-browser'] = TRUE;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 // If not replying to a comment, use our dedicated page callback for new
|
Chris@0
|
117 // Comments on entities.
|
Chris@0
|
118 if (!$comment->id() && !$comment->hasParentComment()) {
|
Chris@0
|
119 $form['#action'] = $this->url('comment.reply', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name]);
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 $comment_preview = $form_state->get('comment_preview');
|
Chris@0
|
123 if (isset($comment_preview)) {
|
Chris@0
|
124 $form += $comment_preview;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 $form['author'] = [];
|
Chris@0
|
128 // Display author information in a details element for comment moderators.
|
Chris@0
|
129 if ($is_admin) {
|
Chris@0
|
130 $form['author'] += [
|
Chris@0
|
131 '#type' => 'details',
|
Chris@0
|
132 '#title' => $this->t('Administration'),
|
Chris@0
|
133 ];
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 // Prepare default values for form elements.
|
Chris@0
|
137 $author = '';
|
Chris@0
|
138 if ($is_admin) {
|
Chris@0
|
139 if (!$comment->getOwnerId()) {
|
Chris@0
|
140 $author = $comment->getAuthorName();
|
Chris@0
|
141 }
|
Chris@0
|
142 $status = $comment->getStatus();
|
Chris@0
|
143 if (empty($comment_preview)) {
|
Chris@0
|
144 $form['#title'] = $this->t('Edit comment %title', [
|
Chris@0
|
145 '%title' => $comment->getSubject(),
|
Chris@0
|
146 ]);
|
Chris@0
|
147 }
|
Chris@0
|
148 }
|
Chris@0
|
149 else {
|
Chris@0
|
150 $status = ($this->currentUser->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED);
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 $date = '';
|
Chris@0
|
154 if ($comment->id()) {
|
Chris@0
|
155 $date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->getCreatedTime());
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 // The uid field is only displayed when a user with the permission
|
Chris@0
|
159 // 'administer comments' is editing an existing comment from an
|
Chris@0
|
160 // authenticated user.
|
Chris@0
|
161 $owner = $comment->getOwner();
|
Chris@0
|
162 $form['author']['uid'] = [
|
Chris@0
|
163 '#type' => 'entity_autocomplete',
|
Chris@0
|
164 '#target_type' => 'user',
|
Chris@0
|
165 '#default_value' => $owner->isAnonymous() ? NULL : $owner,
|
Chris@0
|
166 // A comment can be made anonymous by leaving this field empty therefore
|
Chris@0
|
167 // there is no need to list them in the autocomplete.
|
Chris@0
|
168 '#selection_settings' => ['include_anonymous' => FALSE],
|
Chris@0
|
169 '#title' => $this->t('Authored by'),
|
Chris@0
|
170 '#description' => $this->t('Leave blank for %anonymous.', ['%anonymous' => $config->get('anonymous')]),
|
Chris@0
|
171 '#access' => $is_admin,
|
Chris@0
|
172 ];
|
Chris@0
|
173
|
Chris@0
|
174 // The name field is displayed when an anonymous user is adding a comment or
|
Chris@0
|
175 // when a user with the permission 'administer comments' is editing an
|
Chris@0
|
176 // existing comment from an anonymous user.
|
Chris@0
|
177 $form['author']['name'] = [
|
Chris@0
|
178 '#type' => 'textfield',
|
Chris@0
|
179 '#title' => $is_admin ? $this->t('Name for @anonymous', ['@anonymous' => $config->get('anonymous')]) : $this->t('Your name'),
|
Chris@0
|
180 '#default_value' => $author,
|
Chris@18
|
181 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT),
|
Chris@0
|
182 '#maxlength' => 60,
|
Chris@0
|
183 '#access' => $this->currentUser->isAnonymous() || $is_admin,
|
Chris@0
|
184 '#size' => 30,
|
Chris@0
|
185 '#attributes' => [
|
Chris@0
|
186 'data-drupal-default-value' => $config->get('anonymous'),
|
Chris@0
|
187 ],
|
Chris@0
|
188 ];
|
Chris@0
|
189
|
Chris@0
|
190 if ($is_admin) {
|
Chris@0
|
191 // When editing a comment only display the name textfield if the uid field
|
Chris@0
|
192 // is empty.
|
Chris@0
|
193 $form['author']['name']['#states'] = [
|
Chris@0
|
194 'visible' => [
|
Chris@0
|
195 ':input[name="uid"]' => ['empty' => TRUE],
|
Chris@0
|
196 ],
|
Chris@0
|
197 ];
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 // Add author email and homepage fields depending on the current user.
|
Chris@0
|
201 $form['author']['mail'] = [
|
Chris@0
|
202 '#type' => 'email',
|
Chris@0
|
203 '#title' => $this->t('Email'),
|
Chris@0
|
204 '#default_value' => $comment->getAuthorEmail(),
|
Chris@18
|
205 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT),
|
Chris@0
|
206 '#maxlength' => 64,
|
Chris@0
|
207 '#size' => 30,
|
Chris@0
|
208 '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'),
|
Chris@18
|
209 '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT),
|
Chris@0
|
210 ];
|
Chris@0
|
211
|
Chris@0
|
212 $form['author']['homepage'] = [
|
Chris@0
|
213 '#type' => 'url',
|
Chris@0
|
214 '#title' => $this->t('Homepage'),
|
Chris@0
|
215 '#default_value' => $comment->getHomepage(),
|
Chris@0
|
216 '#maxlength' => 255,
|
Chris@0
|
217 '#size' => 30,
|
Chris@18
|
218 '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT),
|
Chris@0
|
219 ];
|
Chris@0
|
220
|
Chris@0
|
221 // Add administrative comment publishing options.
|
Chris@0
|
222 $form['author']['date'] = [
|
Chris@0
|
223 '#type' => 'datetime',
|
Chris@0
|
224 '#title' => $this->t('Authored on'),
|
Chris@0
|
225 '#default_value' => $date,
|
Chris@0
|
226 '#size' => 20,
|
Chris@0
|
227 '#access' => $is_admin,
|
Chris@0
|
228 ];
|
Chris@0
|
229
|
Chris@0
|
230 $form['author']['status'] = [
|
Chris@0
|
231 '#type' => 'radios',
|
Chris@0
|
232 '#title' => $this->t('Status'),
|
Chris@0
|
233 '#default_value' => $status,
|
Chris@0
|
234 '#options' => [
|
Chris@0
|
235 CommentInterface::PUBLISHED => $this->t('Published'),
|
Chris@0
|
236 CommentInterface::NOT_PUBLISHED => $this->t('Not published'),
|
Chris@0
|
237 ],
|
Chris@0
|
238 '#access' => $is_admin,
|
Chris@0
|
239 ];
|
Chris@0
|
240
|
Chris@0
|
241 return parent::form($form, $form_state, $comment);
|
Chris@0
|
242 }
|
Chris@0
|
243
|
Chris@0
|
244 /**
|
Chris@0
|
245 * {@inheritdoc}
|
Chris@0
|
246 */
|
Chris@0
|
247 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
248 $element = parent::actions($form, $form_state);
|
Chris@0
|
249 /* @var \Drupal\comment\CommentInterface $comment */
|
Chris@0
|
250 $comment = $this->entity;
|
Chris@0
|
251 $entity = $comment->getCommentedEntity();
|
Chris@18
|
252 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
|
Chris@0
|
253 $preview_mode = $field_definition->getSetting('preview');
|
Chris@0
|
254
|
Chris@0
|
255 // No delete action on the comment form.
|
Chris@0
|
256 unset($element['delete']);
|
Chris@0
|
257
|
Chris@0
|
258 // Mark the submit action as the primary action, when it appears.
|
Chris@0
|
259 $element['submit']['#button_type'] = 'primary';
|
Chris@0
|
260
|
Chris@0
|
261 // Only show the save button if comment previews are optional or if we are
|
Chris@0
|
262 // already previewing the submission.
|
Chris@0
|
263 $element['submit']['#access'] = ($comment->id() && $this->currentUser->hasPermission('administer comments')) || $preview_mode != DRUPAL_REQUIRED || $form_state->get('comment_preview');
|
Chris@0
|
264
|
Chris@0
|
265 $element['preview'] = [
|
Chris@0
|
266 '#type' => 'submit',
|
Chris@0
|
267 '#value' => $this->t('Preview'),
|
Chris@0
|
268 '#access' => $preview_mode != DRUPAL_DISABLED,
|
Chris@0
|
269 '#submit' => ['::submitForm', '::preview'],
|
Chris@0
|
270 ];
|
Chris@0
|
271
|
Chris@0
|
272 return $element;
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@0
|
276 * {@inheritdoc}
|
Chris@0
|
277 */
|
Chris@0
|
278 public function buildEntity(array $form, FormStateInterface $form_state) {
|
Chris@0
|
279 /** @var \Drupal\comment\CommentInterface $comment */
|
Chris@0
|
280 $comment = parent::buildEntity($form, $form_state);
|
Chris@0
|
281 if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceof DrupalDateTime) {
|
Chris@0
|
282 $comment->setCreatedTime($form_state->getValue('date')->getTimestamp());
|
Chris@0
|
283 }
|
Chris@0
|
284 else {
|
Chris@0
|
285 $comment->setCreatedTime(REQUEST_TIME);
|
Chris@0
|
286 }
|
Chris@0
|
287 // Empty author ID should revert to anonymous.
|
Chris@0
|
288 $author_id = $form_state->getValue('uid');
|
Chris@0
|
289 if ($comment->id() && $this->currentUser->hasPermission('administer comments')) {
|
Chris@0
|
290 // Admin can leave the author ID blank to revert to anonymous.
|
Chris@0
|
291 $author_id = $author_id ?: 0;
|
Chris@0
|
292 }
|
Chris@0
|
293 if (!is_null($author_id)) {
|
Chris@0
|
294 if ($author_id === 0 && $form['author']['name']['#access']) {
|
Chris@0
|
295 // Use the author name value when the form has access to the element and
|
Chris@0
|
296 // the author ID is anonymous.
|
Chris@0
|
297 $comment->setAuthorName($form_state->getValue('name'));
|
Chris@0
|
298 }
|
Chris@0
|
299 else {
|
Chris@0
|
300 // Ensure the author name is not set.
|
Chris@0
|
301 $comment->setAuthorName(NULL);
|
Chris@0
|
302 }
|
Chris@0
|
303 }
|
Chris@0
|
304 else {
|
Chris@0
|
305 $author_id = $this->currentUser->id();
|
Chris@0
|
306 }
|
Chris@0
|
307 $comment->setOwnerId($author_id);
|
Chris@0
|
308
|
Chris@0
|
309 // Validate the comment's subject. If not specified, extract from comment
|
Chris@0
|
310 // body.
|
Chris@0
|
311 if (trim($comment->getSubject()) == '') {
|
Chris@0
|
312 if ($comment->hasField('comment_body')) {
|
Chris@0
|
313 // The body may be in any format, so:
|
Chris@0
|
314 // 1) Filter it into HTML
|
Chris@0
|
315 // 2) Strip out all HTML tags
|
Chris@0
|
316 // 3) Convert entities back to plain-text.
|
Chris@0
|
317 $comment_text = $comment->comment_body->processed;
|
Chris@0
|
318 $comment->setSubject(Unicode::truncate(trim(Html::decodeEntities(strip_tags($comment_text))), 29, TRUE, TRUE));
|
Chris@0
|
319 }
|
Chris@0
|
320 // Edge cases where the comment body is populated only by HTML tags will
|
Chris@0
|
321 // require a default subject.
|
Chris@0
|
322 if ($comment->getSubject() == '') {
|
Chris@0
|
323 $comment->setSubject($this->t('(No subject)'));
|
Chris@0
|
324 }
|
Chris@0
|
325 }
|
Chris@0
|
326 return $comment;
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 /**
|
Chris@0
|
330 * {@inheritdoc}
|
Chris@0
|
331 */
|
Chris@0
|
332 protected function getEditedFieldNames(FormStateInterface $form_state) {
|
Chris@0
|
333 return array_merge(['created', 'name'], parent::getEditedFieldNames($form_state));
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 /**
|
Chris@0
|
337 * {@inheritdoc}
|
Chris@0
|
338 */
|
Chris@0
|
339 protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
|
Chris@0
|
340 // Manually flag violations of fields not handled by the form display.
|
Chris@0
|
341 foreach ($violations->getByField('created') as $violation) {
|
Chris@0
|
342 $form_state->setErrorByName('date', $violation->getMessage());
|
Chris@0
|
343 }
|
Chris@0
|
344 foreach ($violations->getByField('name') as $violation) {
|
Chris@0
|
345 $form_state->setErrorByName('name', $violation->getMessage());
|
Chris@0
|
346 }
|
Chris@0
|
347 parent::flagViolations($violations, $form, $form_state);
|
Chris@0
|
348 }
|
Chris@0
|
349
|
Chris@0
|
350 /**
|
Chris@0
|
351 * Form submission handler for the 'preview' action.
|
Chris@0
|
352 *
|
Chris@0
|
353 * @param array $form
|
Chris@0
|
354 * An associative array containing the structure of the form.
|
Chris@0
|
355 * @param \Drupal\Core\Form\FormStateInterface $form_state
|
Chris@0
|
356 * The current state of the form.
|
Chris@0
|
357 */
|
Chris@0
|
358 public function preview(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
359 $comment_preview = comment_preview($this->entity, $form_state);
|
Chris@0
|
360 $comment_preview['#title'] = $this->t('Preview comment');
|
Chris@0
|
361 $form_state->set('comment_preview', $comment_preview);
|
Chris@0
|
362 $form_state->setRebuild();
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 /**
|
Chris@0
|
366 * {@inheritdoc}
|
Chris@0
|
367 */
|
Chris@0
|
368 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
369 $comment = $this->entity;
|
Chris@0
|
370 $entity = $comment->getCommentedEntity();
|
Chris@0
|
371 $field_name = $comment->getFieldName();
|
Chris@18
|
372 $uri = $entity->toUrl();
|
Chris@0
|
373 $logger = $this->logger('comment');
|
Chris@0
|
374
|
Chris@0
|
375 if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) {
|
Chris@0
|
376 $comment->save();
|
Chris@0
|
377 $form_state->setValue('cid', $comment->id());
|
Chris@0
|
378
|
Chris@0
|
379 // Add a log entry.
|
Chris@0
|
380 $logger->notice('Comment posted: %subject.', [
|
Chris@0
|
381 '%subject' => $comment->getSubject(),
|
Chris@18
|
382 'link' => $this->l(t('View'), $comment->toUrl()->setOption('fragment', 'comment-' . $comment->id())),
|
Chris@0
|
383 ]);
|
Chris@0
|
384
|
Chris@0
|
385 // Explain the approval queue if necessary.
|
Chris@0
|
386 if (!$comment->isPublished()) {
|
Chris@0
|
387 if (!$this->currentUser->hasPermission('administer comments')) {
|
Chris@17
|
388 $this->messenger()->addStatus($this->t('Your comment has been queued for review by site administrators and will be published after approval.'));
|
Chris@0
|
389 }
|
Chris@0
|
390 }
|
Chris@0
|
391 else {
|
Chris@17
|
392 $this->messenger()->addStatus($this->t('Your comment has been posted.'));
|
Chris@0
|
393 }
|
Chris@0
|
394 $query = [];
|
Chris@0
|
395 // Find the current display page for this comment.
|
Chris@17
|
396 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name];
|
Chris@17
|
397 $page = $this->entityTypeManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page'));
|
Chris@0
|
398 if ($page > 0) {
|
Chris@0
|
399 $query['page'] = $page;
|
Chris@0
|
400 }
|
Chris@0
|
401 // Redirect to the newly posted comment.
|
Chris@0
|
402 $uri->setOption('query', $query);
|
Chris@0
|
403 $uri->setOption('fragment', 'comment-' . $comment->id());
|
Chris@0
|
404 }
|
Chris@0
|
405 else {
|
Chris@0
|
406 $logger->warning('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]);
|
Chris@17
|
407 $this->messenger()->addError($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', ['%subject' => $comment->getSubject()]));
|
Chris@0
|
408 // Redirect the user to the entity they are commenting on.
|
Chris@0
|
409 }
|
Chris@0
|
410 $form_state->setRedirectUrl($uri);
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 }
|