You are here

public function Serializer::render in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Plugin/views/style/Serializer.php \Drupal\rest\Plugin\views\style\Serializer::render()
  2. 10 core/modules/rest/src/Plugin/views/style/Serializer.php \Drupal\rest\Plugin\views\style\Serializer::render()

Render the display in this style.

Overrides StylePluginBase::render

File

core/modules/rest/src/Plugin/views/style/Serializer.php, line 121

Class

Serializer
The style plugin for serialized output formats.

Namespace

Drupal\rest\Plugin\views\style

Code

public function render() {
  $rows = [];

  // If the Data Entity row plugin is used, this will be an array of entities
  // which will pass through Serializer to one of the registered Normalizers,
  // which will transform it to arrays/scalars. If the Data field row plugin
  // is used, $rows will not contain objects and will pass directly to the
  // Encoder.
  foreach ($this->view->result as $row_index => $row) {
    $this->view->row_index = $row_index;
    $rows[] = $this->view->rowPlugin
      ->render($row);
  }
  unset($this->view->row_index);

  // Get the content type configured in the display or fallback to the
  // default.
  if (empty($this->view->live_preview)) {
    $content_type = $this->displayHandler
      ->getContentType();
  }
  else {
    $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
  }
  return $this->serializer
    ->serialize($rows, $content_type, [
    'views_style_plugin' => $this,
  ]);
}