You are here

public function FormatterSingleJson::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 FormatterJson::prepare

File

src/Plugin/formatter/FormatterSingleJson.php, line 34
Contains \Drupal\restful\Plugin\formatter\FormatterSingleJson.

Class

FormatterSingleJson
Class FormatterSingleJson @package Drupal\restful\Plugin\formatter

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 = $this
    ->limitFields($extracted);

  // Force returning a single item.
  $output = ResourceFieldBase::isArrayNumeric($output) ? reset($output) : $output;
  return $output ?: array();
}