public function CommentFormController::submit

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

Overrides EntityFormController::submit

File

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

Class

CommentFormController
Base for controller for comment forms.

Namespace

Drupal\comment

Code

public function submit(array $form, array &$form_state) {
  $comment = parent::submit($form, $form_state);

  // If the comment was posted by a registered user, assign the author's ID.
  // @todo Too fragile. Should be prepared and stored in comment_form()
  // already.
  if (!$comment->is_anonymous && !empty($comment->name->value) && ($account = user_load_by_name($comment->name->value))) {
    $comment->uid->target_id = $account->uid;
  }

  // If the comment was posted by an anonymous user and no author name was
  // required, use "Anonymous" by default.
  if ($comment->is_anonymous && (!isset($comment->name->value) || $comment->name->value === '')) {
    $comment->name->value = config('user.settings')
      ->get('anonymous');
  }

  // Validate the comment's subject. If not specified, extract from comment
  // body.
  if (trim($comment->subject->value) == '') {

    // The body may be in any format, so:
    // 1) Filter it into HTML
    // 2) Strip out all HTML tags
    // 3) Convert entities back to plain-text.
    $comment_text = $comment->comment_body->processed;
    $comment->subject = truncate_utf8(trim(decode_entities(strip_tags($comment_text))), 29, TRUE);

    // Edge cases where the comment body is populated only by HTML tags will
    // require a default subject.
    if ($comment->subject->value == '') {
      $comment->subject->value = t('(No subject)');
    }
  }
  return $comment;
}