public CommentForm::save(array $form, FormStateInterface $form_state)
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- core/modules/comment/src/CommentForm.php, line 348
Class
- CommentForm
- Base handler for comment forms.
Namespace
Drupal\comment
Code
public function save(array $form, FormStateInterface $form_state) { $comment = $this->entity; $entity = $comment->getCommentedEntity(); $field_name = $comment->getFieldName(); $uri = $entity->urlInfo(); $logger = $this->logger('content'); if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) { $comment->save(); $form_state->setValue('cid', $comment->id()); // Add a log entry. $logger->notice('Comment posted: %subject.', array( '%subject' => $comment->getSubject(), 'link' => $this->l(t('View'), $comment->urlInfo()->setOption('fragment', 'comment-' . $comment->id())) )); // Explain the approval queue if necessary. if (!$comment->isPublished()) { if (!$this->currentUser->hasPermission('administer comments')) { drupal_set_message($this->t('Your comment has been queued for review by site administrators and will be published after approval.')); } } else { drupal_set_message($this->t('Your comment has been posted.')); } $query = array(); // Find the current display page for this comment. $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$field_name]; $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $field_definition->getSetting('default_mode'), $field_definition->getSetting('per_page')); if ($page > 0) { $query['page'] = $page; } // Redirect to the newly posted comment. $uri->setOption('query', $query); $uri->setOption('fragment', 'comment-' . $comment->id()); } else { $logger->warning('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->getSubject())); drupal_set_message($this->t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->getSubject())), 'error'); // Redirect the user to the entity they are commenting on. } $form_state->setRedirectUrl($uri); }
Please login to continue.