function ResourceResponseSubscriber::generateContext

Same name in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::generateContext()
  2. 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::generateContext()
  3. 11.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::generateContext()

Generates a top-level JSON:API normalization context.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request from which the context can be derived.

Return value

array The generated context.

1 call to ResourceResponseSubscriber::generateContext()
ResourceResponseSubscriber::renderResponseBody in core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php
Renders a resource response body.

File

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

Class

ResourceResponseSubscriber
Response subscriber that serializes and removes ResourceResponses' data.

Namespace

Drupal\jsonapi\EventSubscriber

Code

protected static function generateContext(Request $request) {
    // Build the expanded context.
    $context = [
        'account' => NULL,
        'sparse_fieldset' => NULL,
    ];
    if ($request->query
        ->has('fields')) {
        $context['sparse_fieldset'] = array_map(function ($item) {
            return explode(',', $item);
        }, $request->query
            ->all('fields'));
    }
    return $context;
}

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