Add new comment
by Joe Fender
Cross-posted with permission from Drupalize.Me
In a continuation from my first post, An Introduction to RESTful Web Services in Drupal 8, I want to explore how Views interacts with REST in Drupal 8.
As many of you already know, the Views module was added to Drupal 8 Core. With RESTful Web Services also in Core, we now have all the tools we need to create highly customisable solutions out of the box.
In this blog post, I will show you how to create a view that returns a list of content in JSON via the REST API. Let’s get started!
First things first, we want to make sure both the REST and Serialization modules are enabled. Then we need to create some dummy content. To do this, grab the latest Drupal 8 development release of Devel module, and enable just the Devel generate submodule. Navigate to Configuration > Development > Generate content, and create a bunch of articles.
Now let’s create our view. We do not need to create a page or block, so uncheck those options to keep things simple
After creating the view, you’ll notice we only have a Master display. For REST to work, we need to add a REST export display.
On the new REST export display, we need to set a path. This is the URL that will be used by clients to return the contents of the view. I’m using the following structure to keep things organised, but you can use anything you like.
REST export displays have a single output format, called Serializer. It converts output into the format the client requests. In other words, when the client calls the URL we set above, they can specify the desired format output. If a format is not specified, Views returns JSON by default. There are 3 formats available: HAL, JSON, and XML. By default the client can request any of these formats, but if you open the Serializer settings dialogue (found in the Format section of the view) you can specify which request formats are accepted.
REST export lets you to return either fields or entities. By setting our view to return entities, we'll get the entire serialised node object. And just like any view, selecting fields lets us pick the fields from the entity we wish to return. For the sake of simplicity here, I've opted to return the content title only.
Remember that we’re using Views, and we have access to it's awesome features. We can set permissions to access the path, sort results by custom criteria and limit the number of results. Views caching is still a work in progress, but once its functional we'll also be able to cache results like any other view. It’s brilliant!
Contextual filters work too. For example we could add the content author UID contextual filter, and clients could then append it to the end of the path (e.g., rest/views/articles/1) to filter results by a specific author. All that's left now is to give it a test run. Make sure to save the view, and then try accessing the URL directly in your browser. As we're not specifying a format, JSON is returned by default.
We can change aliases used for fields by opening the field settings dialogue. This is useful if we want something user friendly and don't want to use default field labels.
I recommend using the Dev HTTP Client chrome extension for testing REST APIs. It will allow you to set the Accept header to return something other than JSON, and it formats the output to assist with debugging. It’s also worth noting that you can only use GET. Views won’t accept POST, PUT, PATCH, or DELETE operations.
I hope this was helpful for those of you who are excited about RESTful Web Services in Drupal 8 Core. Building a REST API for an application has always been tricky. Hopefully with the inclusion of REST and Views in Drupal Core, we’re one step closer to standardising the process.
If you have any questions or comments leave them on Drupalize.Me!