You are here

public function AjaxCommentsController::reply in AJAX Comments 8

Builds ajax response to display a form to reply to another comment.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

\Drupal\Core\Entity\EntityInterface $entity: The entity this comment belongs to.

string $field_name: The field_name to which the comment belongs.

int $pid: The parent comment's comment ID.

Return value

\Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse The Ajax response, or a redirect response if not using ajax.

See also

\Drupal\comment\Controller\CommentController::getReplyForm()

1 string reference to 'AjaxCommentsController::reply'
ajax_comments.routing.yml in ./ajax_comments.routing.yml
ajax_comments.routing.yml

File

src/Controller/AjaxCommentsController.php, line 697

Class

AjaxCommentsController
Controller routines for AJAX comments routes.

Namespace

Drupal\ajax_comments\Controller

Code

public function reply(Request $request, EntityInterface $entity, $field_name, $pid) {
  $is_ajax = Utility::isAjaxRequest($request);
  if ($is_ajax) {
    $response = new AjaxResponse();

    // Get the selectors.
    $selectors = $this->tempStore
      ->getSelectors($request, $overwrite = TRUE);
    $wrapper_html_id = $selectors['wrapper_html_id'];

    // Check the user's access to reply.
    // The user should not have made it this far without proper permission,
    // but adding this access check as a fallback.
    $this
      ->replyAccess($request, $response, $entity, $field_name, $pid);

    // If $this->replyAccess() added any commands to the AjaxResponse,
    // it means that access was denied, so we should NOT ajax load the
    // reply form. Instead, return the response with the error messages
    // immediately.
    if (!empty($response
      ->getCommands())) {
      return $response;
    }

    // Remove any existing status messages and ajax reply forms in the
    // comment field, if applicable.
    $response
      ->addCommand(new RemoveCommand($wrapper_html_id . ' .js-ajax-comments-messages'));
    $response
      ->addCommand(new RemoveCommand($wrapper_html_id . ' .ajax-comments-form-reply'));

    // Build the comment entity form.
    // This approach is very similar to the one taken in
    // \Drupal\comment\CommentLazyBuilders::renderForm().
    $comment = $this
      ->entityTypeManager()
      ->getStorage('comment')
      ->create([
      'entity_id' => $entity
        ->id(),
      'pid' => $pid,
      'entity_type' => $entity
        ->getEntityTypeId(),
      'field_name' => $field_name,
    ]);

    // Build the comment form.
    $form = $this
      ->entityFormBuilder()
      ->getForm($comment);
    $response
      ->addCommand(new AfterCommand(static::getCommentSelectorPrefix() . $pid, $form));

    // Don't delete the tempStore variables here; we need them
    // to persist for the saveReply() method, where the form returned
    // here will be submitted.
    // Instead, return the response without calling $this->tempStore->deleteAll().
    return $response;
  }
  else {

    // If the user attempts to access the comment reply form with JavaScript
    // disabled, degrade gracefully by redirecting to the core comment
    // reply form.
    $redirect = Url::fromRoute('comment.reply', [
      'entity_type' => $entity
        ->getEntityTypeId(),
      'entity' => $entity
        ->id(),
      'field_name' => $field_name,
      'pid' => $pid,
    ])
      ->setAbsolute()
      ->toString();
    $response = new RedirectResponse($redirect);
    return $response;
  }
}