You are here

public function ResponseSubscriber::onResponse in JSON:API Include 8

This method is called the KernelEvents::RESPONSE event is dispatched.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: The filter event.

File

src/EventSubscriber/ResponseSubscriber.php, line 85

Class

ResponseSubscriber
Class ResponseSubscriber.

Namespace

Drupal\jsonapi_include\EventSubscriber

Code

public function onResponse(FilterResponseEvent $event) {
  if (!$this->routeMatch
    ->getRouteObject()) {
    return;
  }
  if (Routes::isJsonApiRequest($this->routeMatch
    ->getRouteObject()
    ->getDefaults())) {
    $response = $event
      ->getResponse();
    if ($response instanceof CacheableResponseInterface) {
      $response
        ->getCacheableMetadata()
        ->addCacheContexts([
        'url.query_args:jsonapi_include',
      ]);
    }
    $need_parse = TRUE;
    if ($this->config
      ->get('jsonapi_include.settings')
      ->get('use_include_query')) {
      $need_parse = !empty($event
        ->getRequest()->query
        ->get('jsonapi_include'));
    }
    if ($need_parse) {
      $content = $event
        ->getResponse()
        ->getContent();
      if (strpos($content, '{"jsonapi"') === 0) {
        $content = $this->jsonapiInclude
          ->parse($content);
        $event
          ->getResponse()
          ->setContent($content);
      }
    }
  }
}