renderResponseBody example


  public function onResponse(ResponseEvent $event) {
    $response = $event->getResponse();
    if (!$response instanceof ResourceResponseInterface) {
      return;
    }

    $request = $event->getRequest();
    $format = $this->getResponseFormat($this->routeMatch, $request);
    $this->renderResponseBody($request$response$this->serializer, $format);
    $event->setResponse($this->flattenResponse($response));
  }

  /** * Determines the format to respond in. * * Respects the requested format if one is specified. However, it is common to * forget to specify a response format in case of a POST or PATCH. Rather than * simply throwing an error, we apply the robustness principle: when POSTing * or PATCHing using a certain format, you probably expect a response in that * same format. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The current route match. * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * * @return string * The response format. */

  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));
  }

  /** * Renders a resource response body. * * Serialization can invoke rendering (e.g., generating URLs), but the * serialization API does not provide a mechanism to collect the * bubbleable metadata associated with that (e.g., language and other * contexts), so instead, allow those to "leak" and collect them here in * a render context. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\jsonapi\ResourceResponse $response * The response from the JSON:API resource. * @param \Symfony\Component\Serializer\SerializerInterface $serializer * The serializer to use. * @param string|null $format * The response format, or NULL in case the response does not need a format, * for example for the response to a DELETE request. * * @todo Add test coverage for language negotiation contexts in * https://www.drupal.org/node/2135829. */
Home | Imprint | This part of the site doesn't use cookies.