You are here

protected function FormatterJsonApi::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

InternalServerErrorException

\Drupal\restful\Exception\ServerConfigurationException

3 calls to FormatterJsonApi::extractFieldValues()
FormatterJsonApi::embedField in src/Plugin/formatter/FormatterJsonApi.php
Embeds the final contents of a field.
FormatterJsonApi::populateCachePlaceholder in src/Plugin/formatter/FormatterJsonApi.php
Given a field item that contains a cache placeholder render and cache it.
FormatterJsonApi::prepare in src/Plugin/formatter/FormatterJsonApi.php
Massages the raw data to create a structured array to pass to the renderer.

File

src/Plugin/formatter/FormatterJsonApi.php, line 106
Contains \Drupal\restful\Plugin\formatter\FormatterJsonApi.

Class

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

Namespace

Drupal\restful\Plugin\formatter

Code

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();
    $output['#fields'] = empty($output['#fields']) ? array() : $output['#fields'];
    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;
    }
    $interpreter = $data
      ->getInterpreter();
    if (!($id_field = $data
      ->getIdField())) {
      throw new ServerConfigurationException('Invalid required ID field for JSON API formatter.');
    }
    $output['#fields'][$public_field_name] = $this
      ->embedField($resource_field, $id_field
      ->render($interpreter), $interpreter, $parents, $parent_hashes);
  }
  if ($data instanceof ResourceFieldCollectionInterface) {
    $output['#resource_name'] = $data
      ->getResourceName();
    $output['#resource_plugin'] = $data
      ->getResourceId();
    $resource_id = $data
      ->getIdField()
      ->render($data
      ->getInterpreter());
    if (!is_array($resource_id)) {

      // In some situations when making an OPTIONS call the $resource_id
      // returns the array of discovery information instead of a real value.
      $output['#resource_id'] = (string) $resource_id;
      try {
        $output['#links']['self'] = restful()
          ->getResourceManager()
          ->getPlugin($output['#resource_plugin'])
          ->versionedUrl($output['#resource_id']);
      } catch (PluginNotFoundException $e) {
      }
    }
  }
  if ($this
    ->isCacheEnabled($data)) {
    $this
      ->setCachedData($data, $output, $parent_hashes);
  }
  return $output;
}