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!