How to use views as REST export in drupal 8

Nitasha BatraFeb 17, 2020
How to use views as REST export in drupal 8

Today, Rest api is a most important & secure way to retreive data in different formats like json, xml etc. to reuse at several places. Suppose we have a shopping website built in drupal 8 and we want to make a native mobile app with simple order functionality. For this, we need listing of products from drupal website in our app. In this case we'll use rest export feature of views which will help us to retrieve all products with different kind of custom filters.

In this artice we have elaborated steps to build a GET type rest api which will return list of products with different kind of filters in json format. But before moving to the steps, we have some specific requirements for implementation of rest api by using views.


Requirements :

a) Restful Web Services Module

This is a core module which comes under "WEB SERVICES" package over extend Page. We must have to install this module with his dependent module i.e. Serialization.

Drupal Rest Export Extend

b) Content Type

We must have a content type with content which we need to get export as rest api.

Here are the steps :

Step 1: Create View with REST Export display

As we all know initial step, we need to create a view by following this path i.e. Admin > Structure > Views > Add new view. In this view we need to checked "Provide a Rest Export" option and add mandatory information about your view and save it.

Drupal Create View

After clicking on "Save and edit", it will redirect to edit page of this view where we'll configure rest of the changes.

Step 2: Configure format

Now, we have to configure format of our view. By default we get two type of formats i.e. json & xml. But by installing one another module i.e. csv serialization, we get one more format i.e. CSV. We can also export data in CSV format.

Drupal Rest Export Format

Step 3: Configure Content

In this step, we'll configure fields from our content type. We can add fields like body, image, category of a content type i.e. product.

Drupal Rest Export Configure Field

Step 4: Configure View Filters

In above steps, we have configured view format & content. Now, we'll add filter on content. Suppose, we need only published products in rest export. So, we'll add filter i.e. Content: Published.

Drupal Rest Export Configure Filter


Step 5: Configure Contextual filter

Suppose we need to search among 1000 products. So, we can a contextual filter which will works by passing query string in Rest Export URL. We'll add this filter on Product title field.

Drupal Rest Export Configure Contextual Filter

Now, here you are ready with RESt export type view in drupal 8 with filters.