You are here

public function JsResponse::setContent in JS Callback Handler 8.3

Return value

$this

1 call to JsResponse::setContent()
JsResponse::setData in src/JsResponse.php
Sets the data returned from a callback.

File

src/JsResponse.php, line 165

Class

JsResponse
JsResponse.

Namespace

Drupal\js

Code

public function setContent($content) {
  if (is_array($content) || $content instanceof \ArrayObject) {
    $json = $content;
    if ($this->js) {

      // Set renderable data as "content".
      if (!$this
        ->isRedirection() && $this
        ->isRenderable($content)) {

        //          // GET requests must be cached by query arguments.
        //          if ($this->js->getRequest()->getMethod() === 'GET') {
        //            $content['#cache']['context'][] = 'url.query_args';
        //          }
        $json = [
          'content' => $this
            ->render($content),
        ];
      }

      // Include general Drupal information on non-redirecting requests.
      if (!$this
        ->isRedirection()) {
        $json['title'] = $this->js
          ->getCallback()
          ->call('getTitle');
        $json['messages'] = drupal_get_messages();
      }

      // Add a reliable response parameter.
      $json['response']['code'] = $this
        ->getStatusCode();
      $json['response']['message'] = isset(static::$statusTexts[$json['response']['code']]) ? static::$statusTexts[$json['response']['code']] : $this
        ->t('Unknown');
    }
    $this->headers
      ->remove('Content-Encoding');
    $content = Json::encode($json);
  }
  parent::setContent($content);
  return $this;
}