You are here

public function AjaxCommentsController::edit in AJAX Comments 8

Returns the comment edit form.

Parameters

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

\Drupal\comment\CommentInterface $comment: The comment entity.

Return value

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

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

File

src/Controller/AjaxCommentsController.php, line 311

Class

AjaxCommentsController
Controller routines for AJAX comments routes.

Namespace

Drupal\ajax_comments\Controller

Code

public function edit(Request $request, CommentInterface $comment) {
  $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'];

    // Hide anchor.
    $response
      ->addCommand(new InvokeCommand('a#comment-' . $comment
      ->id(), 'hide'));

    // Hide comment.
    $response
      ->addCommand(new InvokeCommand(static::getCommentSelectorPrefix() . $comment
      ->id(), 'hide'));

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

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

    // TODO: Get this custom ajax command working later.
    // if (\Drupal::config('ajax_comments.settings')->get('enable_scroll')) {
    //   $response->addCommand(new ajaxCommentsScrollToElementCommand('.ajax-comments-reply-form-' . $comment->getCommentedEntityId() . '-' . $comment->get('pid')->target_id . '-' . $comment->id()));
    // }
    // Don't delete the tempStore variables here; we need them
    // to persist for the save() method below, 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 edit link directly (e.g., at
    // /ajax_comments/1/edit), redirect to the core comment edit form.
    $redirect = Url::fromRoute('entity.comment.edit_form', [
      'comment' => $comment
        ->id(),
    ])
      ->setAbsolute()
      ->toString();
    $response = new RedirectResponse($redirect);
    return $response;
  }
}