function ResourceResponseSubscriber::onResponse

Same name in this branch
  1. 9 core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::onResponse()
Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::onResponse()
  2. 8.9.x core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::onResponse()
  3. 10 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::onResponse()
  4. 10 core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::onResponse()
  5. 11.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::onResponse()
  6. 11.x core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::onResponse()

Serializes ResourceResponse responses' data, and removes that data.

Parameters

\Symfony\Component\HttpKernel\Event\ResponseEvent $event: The event to process.

File

core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php, line 77

Class

ResourceResponseSubscriber
Response subscriber that serializes and removes ResourceResponses' data.

Namespace

Drupal\jsonapi\EventSubscriber

Code

public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!$response instanceof ResourceResponse) {
        return;
    }
    $request = $event->getRequest();
    $format = 'api_json';
    $this->renderResponseBody($request, $response, $this->serializer, $format);
    $event->setResponse($this->flattenResponse($response, $request));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.