You are here

public function HeartbeatSubCommentForm::commentAjaxSubmit in Heartbeat 8

File

src/Form/HeartbeatSubCommentForm.php, line 53

Class

HeartbeatSubCommentForm
Class HeartbeatSubCommentForm.

Namespace

Drupal\heartbeat\Form

Code

public function commentAjaxSubmit(array &$form, FormStateInterface $form_state) {
  if (\Drupal::currentUser()
    ->isAuthenticated()) {
    $commentBody = $form_state
      ->getValue('comment_body');
    $config = \Drupal::config('heartbeat_comment.settings');
    if (strlen(trim($commentBody)) > 1) {
      $comment = Comment::create([
        'entity_type' => 'comment',
        'entity_id' => $config
          ->get('cid'),
        'pid' => $config
          ->get('cid'),
        'field_name' => 'comment',
        'comment_body' => $commentBody,
        'comment_type' => 'comment',
        'subject' => 'Heartbeat Comment',
        'uid' => \Drupal::currentUser()
          ->id(),
      ]);
      if ($comment
        ->save()) {
        $userview = user_view($comment
          ->getOwner(), 'comment');
        $response = new AjaxResponse();
        $response
          ->addCommand(new AppendCommand('#heartbeat-comment-' . $config
          ->get('cid') . ' .sub-comment', '<div class="heartbeat-subcomment" id="sub-comment-' . $comment
          ->id() . '"><span class="comment-owner"><span class="comment-username">' . \Drupal::currentUser()
          ->getAccountName() . '</span>' . render($userview) . '<span class"comment-ago">1 sec ago</span></span><span class="comment-body">' . $commentBody . '</span></div>'));
        return $response;
      }
    }
  }
  return null;
}