You are here

protected function Formatter::limitFields in RESTful 7.2

Returns only the allowed fields by filtering out the other ones.

Parameters

mixed $output: The data structure to filter.

bool|string[] $allowed_fields: FALSE to allow all fields. An array of allowed values otherwise.

Return value

mixed The filtered output.

3 calls to Formatter::limitFields()
FormatterHalJson::prepare in src/Plugin/formatter/FormatterHalJson.php
Massages the raw data to create a structured array to pass to the renderer.
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/Formatter.php, line 264
Contains \Drupal\restful\Plugin\formatter\Formatter

Class

Formatter
Class Formatter.

Namespace

Drupal\restful\Plugin\formatter

Code

protected function limitFields($output, $allowed_fields = NULL) {
  if (!isset($allowed_fields)) {
    $request = ($resource = $this
      ->getResource()) ? $resource
      ->getRequest() : restful()
      ->getRequest();
    $input = $request
      ->getParsedInput();

    // Set the field limits to false if there are no limits.
    $allowed_fields = empty($input['fields']) ? FALSE : explode(',', $input['fields']);
  }
  if (!is_array($output)) {

    // $output is a simple value.
    return $output;
  }
  $result = array();
  if (ResourceFieldBase::isArrayNumeric($output)) {
    foreach ($output as $item) {
      $result[] = $this
        ->limitFields($item, $allowed_fields);
    }
    return $result;
  }
  foreach ($output as $field_name => $field_contents) {
    if ($allowed_fields !== FALSE && !in_array($field_name, $allowed_fields)) {
      continue;
    }
    $result[$field_name] = $this
      ->limitFields($field_contents, $this
      ->unprefixInputOptions($allowed_fields, $field_name));
  }
  return $result;
}