Categories

(83)
(69)
(8)
(34)
(74)
(149)

Configuring REST export with Views in Drupal 8

27.02.2019
Configuring REST export with Views in Drupal 8
Author:

Drupal 8 websites can easily exchange data with third-party websites or apps. These can be iOS or Android devices, applications on Vue.js, React, Angular, or other JS frameworks, and so on. Web services in Drupal 8 core take care of the smooth interaction. To share Drupal data, web developers often use REST export with Views in Drupal 8. Today, we will take a closer look at Views REST export.

REST export with Views in Drupal 8

The Views module is in Drupal 8 core, and lets us quickly create collections of content, users, taxonomy terms, and more. We can organize Drupal data into a View and prepare it for export in the JSON format needed by third-party apps. As a result, we will have a RESTful API endpoint, where other resources can call to get our data.

Enabling the Drupal 8 modules for REST export

To provide REST export with Views in Drupal 8, we begin with enabling the core “Web services” modules. Two of the four will be enough for today’s example — RESTful Web Services and Serialization.

Creating a RESTful View

We are going to share Drupal content. For this, a custom content type or a default one (“Article” or “Basic page”) will be OK. Let’s use “Article.” We can create a couple of test articles (Article 1, Article 2, Article 3) and then move on to the View creation.

In the “Structure — Views” tab of our site’s admin, we click “Add new view,” and do 3 things:

  • Give our View a name.
  • Check “Provide a REST export” option (which we see next to the usual “Create a page” and “Create a block” options).
  • Write a REST export path (where other resources can call to get our content).

Configuring the RESTful View

Our RESTful View looks almost like the classic version, but the output is in JSON. It has standard Views capabilities, so we can:

  • choose to display fields, entity, or search results
  • do filter and sorting
  • limit the amount of results
  • create contextual filters

and much more.

Let’s filter our View to only show the “Article” content type.

Now we choose to display fields:

And we add a couple of fields to the View — “title” and “body.” It is also possible to specify a custom URL path for each field in the field settings, if we for some reason do not want to use their default ones.

An unusual but important element in the RESTful Views interface is the “Serializer” tool in the “Format” section. In its “Settings,” we can select the formats to output our content in.

In our case, we have a choice between JSON and XML (other modules would add more formats). If no format is chosen, the default output will be in JSON.

Let’s save our View. The data in JSON is available through the URL path that we provided (json/content). This is our REST API endpoint, and it has this structure:

website-example/json/content.

We receive JSON output in ASCII format that is encoded for security reasons. Our JSON is ready to be sent to third-party apps and then decoded by them.

To check if the code validates, as well as to see how it looks in a more “attractive” way, we can use a Chrome app or one of online JSON formatters & validators.

Here’s how one of them processes our code:

Another JSON formatter does more decoding for our JSON better, and makes it even more human-readable:

JSON validates, so our View is ready to share content. We have successfully configured REST export with Views in Drupal 8.

Let’s create the desired export for your Drupal website

Although the REST architecture supports CRUD (create, read, update, and delete) operations, Views is only for R (reading).

So if you want mobile app users to be able to edit your Drupal 8 content, REST export with Views in Drupal 8 will not do for you. We could offer you a different solution then like a full REST API in the JSON format, custom endpoints, and so on.

Whatever are your ideas about third-party integration, our Drupal company will help you implement them!

3 votes, Rating: 5

Read also

1

Various forms are the heart of website’s interaction with the user. So form building tools are in demand — the Drupal Webform module ranks 7th on the 42,000+ list of contributed modules. The...

2

Everyone loves attractive layouts for web pages. Luckily, Drupal has plenty of awesome page building tools. Our Drupal team loves to take the best of them from our arsenal, combine them when...

3

What is the first thing that grabs your attention when you visit an E-commerce website? No doubt it is the website’s design and appearance. Which E-commerce themes are the best? Don’t worry....

4

Drupal 8 is on its peak of flourishing, but the cycles of development never stop. That’s why the Drupal community has already announced the expected release of Drupal 9 and end-of-life for Drupal...

5

Our web development team prefers Drupal 8 content management system (CMS) for multilingual websites. Now find out what features make Drupal 8 the perfect choice for a multilingual website...

Subscribe to our blog updates