diff --git a/src/Plugin/views/display/RestExportNested.php b/src/Plugin/views/display/RestExportNested.php index a852577..03f52b1 100644 --- a/src/Plugin/views/display/RestExportNested.php +++ b/src/Plugin/views/display/RestExportNested.php @@ -33,22 +33,18 @@ class RestExportNested extends RestExport { return $this->view->style_plugin->render(); }); - $class = $this->renderer->executeInRenderContext(new RenderContext(), function () { - return get_class($this->view->style_plugin); - }); - // Decode results. $results = \GuzzleHttp\json_decode($build['#markup']); - switch ($class) { - case 'Drupal\pager_serializer\Plugin\views\style\PagerSerializer': - $rows = \Drupal::config('pager_serializer.settings')->get('rows_label'); - $results->$rows = $this->flatten($results->$rows); - break; - case 'Drupal\rest\Plugin\views\style\Serializer': - default: - $results = $this->flatten($results); - break; + // The Core Serializer returns an array of rows. Custom can create an object + // with a 'rows' parameter. + if (gettype($results) === 'object') { + // @todo Allow rows name property to be customizable. + $rows = 'rows'; + $results->$rows = $this->flatten($results->$rows); + } + else { + $results = $this->flatten($results); } // Convert back to JSON. @@ -93,11 +89,12 @@ class RestExportNested extends RestExport { * Flattened view data. */ protected function flatten(array $data) { + // Loop through rows results and fields. foreach ($data as $key => $result) { foreach ($result as $property => $value) { // Check if the field can be decoded using PHP's json_decode(). if (is_string($value)) { - if (json_decode($value) !== $value) { + if (json_decode($value) !== NULL) { // If so, use Guzzle to decode the JSON and add it to the data. $data[$key]->$property = \GuzzleHttp\json_decode($value); }