You are here

public function FormatterJson::prepare in RESTful 7.2

Massages the raw data to create a structured array to pass to the renderer.

Parameters

ResourceFieldInterface[] $data: The raw data to return.

Return value

array The data prepared to be rendered.

Overrides FormatterInterface::prepare

1 method overrides FormatterJson::prepare()
FormatterSingleJson::prepare in src/Plugin/formatter/FormatterSingleJson.php
Massages the raw data to create a structured array to pass to the renderer.

File

src/Plugin/formatter/FormatterJson.php, line 39
Contains \Drupal\restful\Plugin\formatter\FormatterJson.

Class

FormatterJson
Class FormatterHalJson.

Namespace

Drupal\restful\Plugin\formatter

Code

public function prepare(array $data) {

  // If we're returning an error then set the content type to
  // 'application/problem+json; charset=utf-8'.
  if (!empty($data['status']) && floor($data['status'] / 100) != 2) {
    $this->contentType = 'application/problem+json; charset=utf-8';
    return $data;
  }
  $extracted = $this
    ->extractFieldValues($data);
  $output = array(
    'data' => $this
      ->limitFields($extracted),
  );
  if ($resource = $this
    ->getResource()) {
    $request = $resource
      ->getRequest();
    $data_provider = $resource
      ->getDataProvider();
    if ($request
      ->isListRequest($resource
      ->getPath())) {

      // Get the total number of items for the current request without
      // pagination.
      $output['count'] = $data_provider
        ->count();

      // If there are items that were taken out during access checks,
      // report them as denied in the metadata.
      if (variable_get('restful_show_access_denied', FALSE) && ($inaccessible_records = $data_provider
        ->getMetadata()
        ->get('inaccessible_records'))) {
        $output['denied'] = empty($output['meta']['denied']) ? $inaccessible_records : $output['meta']['denied'] + $inaccessible_records;
      }
    }
    if (method_exists($resource, 'additionalHateoas')) {
      $output = array_merge($output, $resource
        ->additionalHateoas($output));
    }

    // Add HATEOAS to the output.
    $this
      ->addHateoas($output);
  }
  return $output;
}