public function CommentFormController::save

Overrides Drupal\Core\Entity\EntityFormController::save().

Overrides EntityFormController::save

File

drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php, line 338
Definition of Drupal\comment\CommentFormController.

Class

CommentFormController
Base for controller for comment forms.

Namespace

Drupal\comment

Code

public function save(array $form, array &$form_state) {
  $node = node_load($form_state['values']['nid']);
  $comment = $this
    ->getEntity($form_state);
  if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {

    // Save the anonymous user information to a cookie for reuse.
    if (user_is_anonymous()) {
      user_cookie_save(array_intersect_key($form_state['values'], array_flip(array(
        'name',
        'mail',
        'homepage',
      ))));
    }
    comment_save($comment);
    $form_state['values']['cid'] = $comment->cid;

    // Add an entry to the watchdog log.
    watchdog('content', 'Comment posted: %subject.', array(
      '%subject' => $comment->subject,
    ), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->cid, array(
      'fragment' => 'comment-' . $comment->cid,
    )));

    // Explain the approval queue if necessary.
    if ($comment->status == COMMENT_NOT_PUBLISHED) {
      if (!user_access('administer comments')) {
        drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
      }
    }
    else {
      drupal_set_message(t('Your comment has been posted.'));
    }
    $query = array();

    // Find the current display page for this comment.
    $page = comment_get_display_page($comment->cid, $node->type);
    if ($page > 0) {
      $query['page'] = $page;
    }

    // Redirect to the newly posted comment.
    $redirect = array(
      'node/' . $node->nid,
      array(
        'query' => $query,
        'fragment' => 'comment-' . $comment->cid,
      ),
    );
  }
  else {
    watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array(
      '%subject' => $comment->subject,
    ), WATCHDOG_WARNING);
    drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array(
      '%subject' => $comment->subject,
    )), 'error');

    // Redirect the user to the node they are commenting on.
    $redirect = 'node/' . $node->nid;
  }
  $form_state['redirect'] = $redirect;

  // Clear the block and page caches so that anonymous users see the comment
  // they have posted.
  cache_invalidate_tags(array(
    'content' => TRUE,
  ));
}