As of now there is no way to display total page count in views rest export. But you can get total pages, current page, items per page and total number of items using new custom style in your custom module which extends existing Serializer
class as shown below:
<?php
namespace Drupal\custom_module\Plugin\views\style;
use Drupal\rest\Plugin\views\style\Serializer;
/**
* The style plugin for serialized output formats.
*
* @ingroup views_style_plugins
*
* @ViewsStyle(
* id = "custom_serializer",
* title = @Translation("Custom serializer"),
* help = @Translation("Serializes views row data and pager using the Serializer component."),
* display_types = {"data"}
* )
*/
class CustomSerializer extends Serializer {
/**
* {@inheritdoc}
*/
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';
}
$pager = $this->view->pager;
$class = get_class($pager);
$current_page = $pager->getCurrentPage();
$items_per_page = $pager->getItemsPerPage();
$total_items = $pager->getTotalItems();
$total_pages = 0;
if(!in_array($class, ['Drupal\views\Plugin\views\pager\None', 'Drupal\views\Plugin\views\pager\Some'])){
$total_pages = $pager->getPagerTotal();
}
$result = [
'rows' => $rows,
'pager' => [
'current_page' => $current_page,
'total_items' => $total_items,
'total_pages' => $total_pages,
'items_per_page' => $items_per_page,
],
];
return $this->serializer->serialize($result, $content_type, ['views_style_plugin' => $this]);
}
}
After adding above file clear drupal cache and edit the view. There you need to change the format from Serializer
to Custom serializer
.
In this style there is a little difference in the output. All rows are children of rows
and pager information is part of pager
. Sample output is as shown below:
{
"rows":[
{
"title":"Test"
},
{
"title":"Test"
},
{
"title":"Title 5"
},
{
"title":"Title 4"
},
{
"title":"Title 3"
}
],
"pager":{
"current_page":0,
"total_items":6,
"total_pages":2,
"items_per_page":5
}
}