You are here

protected function FormatterJsonApi::populateCachePlaceholder in RESTful 7.2

Given a field item that contains a cache placeholder render and cache it.

Parameters

array $field_item: The output to render.

string $includes_path: The includes path encoded in dot notation.

Return value

array The rendered embedded field item.

Throws

\Drupal\restful\Exception\BadRequestException

\Drupal\restful\Exception\InternalServerErrorException

1 call to FormatterJsonApi::populateCachePlaceholder()
FormatterJsonApi::renormalize in src/Plugin/formatter/FormatterJsonApi.php
Move the embedded resources to the included key.

File

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

Class

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

Namespace

Drupal\restful\Plugin\formatter

Code

protected function populateCachePlaceholder(array $field_item, $includes_path) {
  if (empty($field_item['#cache_placeholder']) || empty($field_item['#resource_id']) || empty($field_item['#resource_plugin'])) {
    return $field_item;
  }
  $embedded_resource = restful()
    ->getResourceManager()
    ->getPluginCopy($field_item['#resource_plugin']);
  $input = $this
    ->getRequest()
    ->getParsedInput();
  $new_input = $input + array(
    'include' => '',
    'fields' => '',
  );

  // If the field is not supposed to be included, then bail.
  $old_includes = array_filter(explode(',', $new_input['include']));
  if (!in_array($includes_path, $old_includes)) {
    return $field_item;
  }
  $new_input['fields'] = implode(',', $this
    ->unprefixInputOptions(explode(',', $new_input['fields']), $includes_path));
  $new_input['include'] = implode(',', $this
    ->unprefixInputOptions($old_includes, $includes_path));

  // Create a new request from scratch copying most of the values but the
  // $query.
  $embedded_resource
    ->setRequest(Request::create($this
    ->getRequest()
    ->getPath(), array_filter($new_input), $this
    ->getRequest()
    ->getMethod(), $this
    ->getRequest()
    ->getHeaders(), $this
    ->getRequest()
    ->isViaRouter(), $this
    ->getRequest()
    ->getCsrfToken(), $this
    ->getRequest()
    ->getCookies(), $this
    ->getRequest()
    ->getFiles(), $this
    ->getRequest()
    ->getServer(), $this
    ->getRequest()
    ->getParsedBody()));
  try {
    $data = $embedded_resource
      ->getDataProvider()
      ->view($field_item['#resource_id']);
  } catch (InaccessibleRecordException $e) {

    // Populate it with an empty element.
    $data = array();
  }
  return array_merge($field_item, $this
    ->extractFieldValues($data, $field_item['#cache_placeholder']['parents'], $field_item['#cache_placeholder']['parent_hashes']));
}