Accueil / Blog / Your First RESTful View in Drupal 8
Accueil / Blog / Your First RESTful View in Drupal 8

Your First RESTful View in Drupal 8

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.

small photo

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.

next photo

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.

photo 7

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!

Ajouter un commentaire

Plain text

  • Aucune balise HTML autorisée.
  • Les adresses de pages web et de courriels sont transformées en liens automatiquement.
  • Les lignes et les paragraphes vont à la ligne automatiquement.

Filtered HTML

  • Use [acphone_sales], [acphone_sales_text], [acphone_support], [acphone_international], [acphone_devcloud], [acphone_extra1] and [acphone_extra2] as placeholders for Acquia phone numbers. Add class "acquia-phones-link" to wrapper element to make number a link.
  • Pour publier des morceaux de code, entourez-les avec les balises <code>...</code>. Pour du PHP, utilisez. <?php ... ?>, ce qui va colorier le code en fonction de sa syntaxe.
  • Les adresses de pages web et de courriels sont transformées en liens automatiquement.
  • Tags HTML autorisés : <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <h4> <h5> <h2> <img>
  • Les lignes et les paragraphes vont à la ligne automatiquement.
By submitting this form, you accept the Mollom privacy policy.