protected function FormatterHalJson::addHateoas in RESTful 7.2
Add HATEOAS links to list of item.
Parameters
array $data: The data array after initial massaging.
1 call to FormatterHalJson::addHateoas()
- FormatterHalJson::prepare in src/
Plugin/ formatter/ FormatterHalJson.php - Massages the raw data to create a structured array to pass to the renderer.
File
- src/
Plugin/ formatter/ FormatterHalJson.php, line 107 - Contains \Drupal\restful\Plugin\formatter\FormatterHalJson.
Class
- FormatterHalJson
- Class FormatterHalJson @package Drupal\restful\Plugin\formatter
Namespace
Drupal\restful\Plugin\formatterCode
protected function addHateoas(array &$data) {
if (!($resource = $this
->getResource())) {
return;
}
$request = $resource
->getRequest();
if (!isset($data['_links'])) {
$data['_links'] = array();
}
// Get self link.
$data['_links']['self'] = array(
'title' => 'Self',
'href' => $resource
->versionedUrl($resource
->getPath()),
);
$input = $request
->getPagerInput();
$page = $input['number'];
if ($page > 1) {
$input['number'] = $page - 1;
$data['_links']['previous'] = array(
'title' => 'Previous',
'href' => $resource
->getUrl(),
);
}
$curies_resource = $this
->withCurie($resource
->getResourceMachineName());
$listed_items = empty($data['_embedded'][$curies_resource]) ? 1 : count($data['_embedded'][$curies_resource]);
// We know that there are more pages if the total count is bigger than the
// number of items of the current request plus the number of items in
// previous pages.
$items_per_page = $this
->calculateItemsPerPage($resource);
$previous_items = ($page - 1) * $items_per_page;
if (isset($data['count']) && $data['count'] > $listed_items + $previous_items) {
$input['number'] = $page + 1;
$data['_links']['next'] = array(
'title' => 'Next',
'href' => $resource
->getUrl(),
);
}
if (!($curie = $this
->getCurie())) {
return;
}
$curie += array(
'path' => 'doc/rels',
'template' => '/{rel}',
);
$data['_links']['curies'] = array(
'name' => $curie['name'],
'href' => url($curie['path'], array(
'absolute' => TRUE,
)) . $curie['template'],
'templated' => TRUE,
);
}