generateContext example


  protected function renderResponseBody(Request $request, ResourceResponse $response, SerializerInterface $serializer$format) {
    $data = $response->getResponseData();

    // If there is data to send, serialize and set it as the response body.     if ($data !== NULL) {
      // First normalize the data. Note that error responses do not need a       // normalization context, since there are no entities to normalize.       // @see \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::isJsonApiExceptionEvent()       $context = !$response->isSuccessful() ? [] : static::generateContext($request);
      $jsonapi_doc_object = $serializer->normalize($data$format$context);
      // Having just normalized the data, we can associate its cacheability with       // the response object.       if ($response instanceof CacheableResponseInterface) {
        assert($jsonapi_doc_object instanceof CacheableNormalization);
        $response->addCacheableDependency($jsonapi_doc_object);
      }
      // Finally, encode the normalized data (JSON:API's encoder rasterizes it       // automatically).       $response->setContent($serializer->encode($jsonapi_doc_object->getNormalization()$format));
      $response->headers->set('Content-Type', $request->getMimeType($format));
    }
Home | Imprint | This part of the site doesn't use cookies.