protected function FormatterJson::extractFieldValues in RESTful 7.2
Extracts the actual values from the resource fields.
Parameters
array[]|ResourceFieldCollectionInterface $data: The array of rows or a ResourceFieldCollection.
string[] $parents: An array that holds the name of the parent fields that lead to the current data structure.
string[] $parent_hashes: An array that holds the name of the parent cache hashes that lead to the current data structure.
Return value
array[] The array of prepared data.
Throws
2 calls to FormatterJson::extractFieldValues()
- FormatterJson::prepare in src/
Plugin/ formatter/ FormatterJson.php - Massages the raw data to create a structured array to pass to the renderer.
- 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 91 - Contains \Drupal\restful\Plugin\formatter\FormatterJson.
Class
- FormatterJson
- Class FormatterHalJson.
Namespace
Drupal\restful\Plugin\formatterCode
protected function extractFieldValues($data, array $parents = array(), array &$parent_hashes = array()) {
$output = array();
if ($this
->isCacheEnabled($data)) {
$parent_hashes[] = $this
->getCacheHash($data);
if ($cache = $this
->getCachedData($data)) {
return $cache->data;
}
}
foreach ($data as $public_field_name => $resource_field) {
if (!$resource_field instanceof ResourceFieldInterface) {
// If $resource_field is not a ResourceFieldInterface it means that we
// are dealing with a nested structure of some sort. If it is an array
// we process it as a set of rows, if not then use the value directly.
$parents[] = $public_field_name;
$output[$public_field_name] = static::isIterable($resource_field) ? $this
->extractFieldValues($resource_field, $parents, $parent_hashes) : $resource_field;
continue;
}
if (!$data instanceof ResourceFieldCollectionInterface) {
throw new InternalServerErrorException('Inconsistent output.');
}
// This feels a bit awkward, but if the result is going to be cached, it
// pays off the extra effort of generating the whole resource entity. That
// way we can get a different field set with the previously cached entity.
// If the entity is not going to be cached, then avoid generating the
// field data altogether.
$limit_fields = $data
->getLimitFields();
if (!$this
->isCacheEnabled($data) && $limit_fields && !in_array($resource_field
->getPublicName(), $limit_fields)) {
// We are not going to cache this and this field is not in the output.
continue;
}
$value = $resource_field
->render($data
->getInterpreter());
// If the field points to a resource that can be included, include it
// right away.
if (static::isIterable($value) && $resource_field instanceof ResourceFieldResourceInterface) {
$value = $this
->extractFieldValues($value, $parents, $parent_hashes);
}
$output[$public_field_name] = $value;
}
if ($this
->isCacheEnabled($data)) {
$this
->setCachedData($data, $output, $parent_hashes);
}
return $output;
}