You are here

protected function RestfulFormatterHalJson::moveReferencesToEmbeds in RESTful 7

Move the fields referencing other resources to the _embed key.

Note that for multiple value entityreference fields $row[$public_field_name] will be an array of values rather than a single value.

Parameters

array $embedded: Embedded array to be modified.

array $row: The row being processed.

array $public_field: The public field configuration array.

string $public_field_name: The name of the public field.

array $output: Output array to be modified.

1 call to RestfulFormatterHalJson::moveReferencesToEmbeds()
RestfulFormatterHalJson::prepareRow in plugins/formatter/hal_json/RestfulFormatterHalJson.class.php
Massage the data of a single row.

File

plugins/formatter/hal_json/RestfulFormatterHalJson.class.php, line 251
Contains RestfulFormatterHalJson.

Class

RestfulFormatterHalJson
@file Contains RestfulFormatterHalJson.

Code

protected function moveReferencesToEmbeds(array &$embedded, array &$row, $public_field, $public_field_name, array &$output) {
  $values_metadata = $this->handler
    ->getValueMetadata($row['id'], $public_field_name);

  // Wrap the row in an array if it isn't.
  if (!is_array($row[$public_field_name])) {
    $row[$public_field_name] = array();
  }
  $rows = RestfulBase::isArrayNumeric($row[$public_field_name]) ? $row[$public_field_name] : array(
    $row[$public_field_name],
  );
  foreach ($rows as $subindex => $subrow) {
    $metadata = $values_metadata[$subindex];

    // Loop through each value for the field.
    foreach ($subrow as $index => $resource_row) {
      if (empty($metadata[$index])) {

        // No metadata.
        continue;
      }

      // If there is no resource name in the metadata for this particular
      // value, assume that we are referring to the first resource in the
      // field definition.
      $resource_name = NULL;
      if (!empty($metadata['resource_name'])) {

        // Make sure that the resource in the metadata exists in the list of
        // resources available for this particular public field.
        foreach ($public_field['resource'] as $resource) {
          if ($resource['name'] != $metadata['resource_name']) {
            continue;
          }
          $resource_name = $metadata['resource_name'];
        }
      }
      if (empty($resource_name)) {
        $resource = reset($public_field['resource']);
        $resource_name = $resource['name'];
      }
      $curies_resource = $this
        ->withCurie($resource_name);
      $prepared_row = $this
        ->prepareRow($subrow, $output);
      if ($this->handler
        ->isListRequest()) {
        $embedded[$curies_resource][] = $prepared_row;
      }
      else {
        $output['_embedded'][$curies_resource][] = $prepared_row;
      }
    }
  }

  // Remove the original reference.
  unset($row[$public_field_name]);
}