You are here

public function SocialPostAlbumAjaxCommentsController::socialAdd in Open Social 10.1.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_post/modules/social_post_album/src/Controller/SocialPostAlbumAjaxCommentsController.php \Drupal\social_post_album\Controller\SocialPostAlbumAjaxCommentsController::socialAdd()
  2. 10.0.x modules/social_features/social_post/modules/social_post_album/src/Controller/SocialPostAlbumAjaxCommentsController.php \Drupal\social_post_album\Controller\SocialPostAlbumAjaxCommentsController::socialAdd()
  3. 10.2.x modules/social_features/social_post/modules/social_post_album/src/Controller/SocialPostAlbumAjaxCommentsController.php \Drupal\social_post_album\Controller\SocialPostAlbumAjaxCommentsController::socialAdd()

Builds ajax response for adding a new comment without a parent comment.

This is copied from AjaxCommentsController::add because a reply on a reply is using the add new Form with a mention. While Ajax comments uses the save function for a reply. This results in status message not being rendered correctly. The only change here is the addMessage is placed above the reply.

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: (optional) Some comments are replies to other comments. In those cases, $pid is the parent comment's comment ID. Defaults to NULL.

Return value

\Drupal\Core\Ajax\AjaxResponse The Ajax response.

Overrides AjaxCommentsController::socialAdd

See also

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

File

modules/social_features/social_post/modules/social_post_album/src/Controller/SocialPostAlbumAjaxCommentsController.php, line 23

Class

SocialPostAlbumAjaxCommentsController
Controller routines for AJAX comments routes.

Namespace

Drupal\social_post_album\Controller

Code

public function socialAdd(Request $request, EntityInterface $entity, $field_name, $pid = NULL) {
  $this->clearTempStore = FALSE;
  $response = parent::socialAdd($request, $entity, $field_name, $pid);
  if ($this->errors !== 0) {
    if ($this->errors !== NULL) {
      $this->tempStore
        ->deleteAll();
    }
    return $response;
  }
  $comment = $this
    ->entityTypeManager()
    ->getStorage('comment')
    ->create([
    'entity_id' => $entity
      ->id(),
    'pid' => $pid,
    'entity_type' => $entity
      ->getEntityTypeId(),
    'field_name' => $field_name,
  ]);
  $form = $this
    ->entityFormBuilder()
    ->getForm($comment);
  $this->tempStore
    ->setSelector('form_html_id', $form['#attributes']['id']);
  $field = $this
    ->renderCommentField($entity, $field_name);
  $field['#attributes']['id'] .= self::WRAPPER_ID_SUFFIX;
  $selectors = $this->tempStore
    ->getSelectors($request);
  $response
    ->addCommand(new ReplaceCommand($selectors['wrapper_html_id'] . self::WRAPPER_ID_SUFFIX, $field), TRUE);
  $this->tempStore
    ->deleteAll();
  return $response;
}