You are here

protected function AjaxController::getNewPrivateMessages in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Controller/AjaxController.php \Drupal\private_message\Controller\AjaxController::getNewPrivateMessages()

Creates an Ajax Command containing new private message.

Parameters

Drupal\Core\Ajax\AjaxResponse $response: The response to which any commands should be attached.

1 call to AjaxController::getNewPrivateMessages()
AjaxController::ajaxCallback in src/Controller/AjaxController.php
Create AJAX responses for JavaScript requests.

File

src/Controller/AjaxController.php, line 190

Class

AjaxController
Controller to handle Ajax requests.

Namespace

Drupal\private_message\Controller

Code

protected function getNewPrivateMessages(AjaxResponse $response) {
  $thread_id = $this->requestStack
    ->getCurrentRequest()
    ->get('threadid');
  $message_id = $this->requestStack
    ->getCurrentRequest()
    ->get('messageid');
  if (is_numeric($thread_id) && is_numeric($message_id)) {
    $new_messages = $this->privateMessageService
      ->getNewMessages($thread_id, $message_id);
    if (count($new_messages)) {
      $messages = [];
      $view_builder = $this->entityManager
        ->getViewBuilder('private_message');
      foreach ($new_messages as $message) {
        if ($message
          ->access('view', $this->currentUser)) {
          $messages[] = $view_builder
            ->view($message);
        }
      }
      $response
        ->addCommand(new PrivateMessageInsertNewMessagesCommand($this->renderer
        ->renderRoot($messages)));
    }
  }
}