public function FormatterJsonApi::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
File
- src/
Plugin/ formatter/ FormatterJsonApi.php, line 46 - Contains \Drupal\restful\Plugin\formatter\FormatterJsonApi.
Class
- FormatterJsonApi
- Class FormatterJsonApi @package Drupal\restful\Plugin\formatter
Namespace
Drupal\restful\Plugin\formatterCode
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);
$included = array();
$output = array(
'data' => $this
->renormalize($extracted, $included),
);
$output = $this
->populateIncludes($output, $included);
if ($resource = $this
->getResource()) {
$request = $resource
->getRequest();
$data_provider = $resource
->getDataProvider();
$is_list_request = $request
->isListRequest($resource
->getPath());
if ($is_list_request) {
// Get the total number of items for the current request without
// pagination.
$output['meta']['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['meta']['denied'] = empty($output['meta']['denied']) ? $inaccessible_records : $output['meta']['denied'] + $inaccessible_records;
}
}
else {
// For non-list requests do not return an array of one item.
$output['data'] = reset($output['data']);
}
if (method_exists($resource, 'additionalHateoas')) {
$output = array_merge($output, $resource
->additionalHateoas($output));
}
// Add HATEOAS to the output.
$this
->addHateoas($output);
}
return $output;
}