You are here

public function PagerSerializer::render in Pager Serializer 8

Render the display in this style.

Overrides Serializer::render

File

src/Plugin/views/style/PagerSerializer.php, line 44

Class

PagerSerializer
The style plugin for serialized output formats.

Namespace

Drupal\pager_serializer\Plugin\views\style

Code

public function render() {
  $rows = [];
  $config = \Drupal::config(static::SETTINGS);
  $rows_label = $config
    ->get('rows_label');
  $use_pager = $config
    ->get('pager_object_enabled');

  // 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 (method_exists($this->displayHandler, 'getContentType')) {
    $content_type = $this->displayHandler
      ->getContentType();
  }
  else {
    $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
  }
  $pagination = $this
    ->pagination($config, $rows);
  if ($use_pager) {
    $pager_label = $config
      ->get('pager_label');
    $result = [
      $rows_label => $rows,
      $pager_label => $pagination,
    ];
  }
  else {
    $result = $pagination;
    $result[$rows_label] = $rows;
  }
  return $this->serializer
    ->serialize($result, $content_type, [
    'views_style_plugin' => $this,
  ]);
}