3

I have a paginated views using "rest export" and I'm using it to show that data on mobile app, everything works fine, but I want to know how many pages exist on that endpoint to show page numbers in my app.

I know I can alter the view programmatically, but I'm asking if there is a simple solution to achieve this.

This is the view settings page.

enter image description here

7

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
   }
}
| improve this answer | |
  • For newbies like me, this needs to be at modules/custom/yourmodule/src/Plugin/views/style/SerializerName.php – Abdul Sadik Yalcin Jan 8 at 17:15
  • @vishal-patil your answer inspired me to write the Pager Serializer module. – Binary Alchemist Feb 29 at 19:54
  • @BinaryAlchemist I am glad it helped you create a module. If it is released you can add a link in comments to help others. – Vishal Patil May 8 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.