Drupal 8: RESTful Services

Senior Drupal Architect

Drupal 8: RESTful Services

Mediacurrent Drupal Presentaiton

Our next webinar in the six part series on Drupal 8 is given by Peter Mallett.

In this webinar Peter summarizes the meaning of Services in Drupal 8, why the initiative is happening, a few features with a demo, and some of the rules of REST. The demo is running on a pre-alpha 3 development snapshot of Drupal 8.



Peter elaborates on a question heard from the presentation that asked if it would be possible to create custom services endpoints that still utilized "verbs" in the resource URI. His answer stated that you should encapsulate the logic elsewhere in the application, so that the service resources would only need to be used with POST, PATCH, etc. This example will better illustrate what he was trying to convey:

Imagine we have resources: teacher, class, student, assignment, and task.

One thing we might want to do is assign a task to every student in a teacher's class based on an assignment entity. One way to do this, and sadly I admit I have done this, would be to simply create a resource for /create-assignment, and pass it the assignment object. Next, the service would need to look up the teacher, class, and student. Then, the service would create a task for each student. This is not Restful at all.

A much better way to do this would be:
1) A teacher user clicks "assign task to students" on the assignment screen
2) The application makes a GET request to the resource for the assignment's class and receives the class resource, which contains a list of the students in that class
3) The application could then ask for confirmation to "assign task to X number of students?"
4) The user confirms the assignment
5) The application constructs a task entity for each student and makes a POST to the task URI to create the new task resources

In the end we have the same result. However, now the service side of the scenario remains restful. The logic of creating and assigning the tasks to the students all resides in the application instead of being embedded in the service.

Richardson Maturity Model - resource 1

Richardson Maturity Model - resource 2

HAL - Hypertext Application Language

D7 views_datasource

Rest and Serialization in Drupal 8 - DrupalCon Portland

The current d.o REST documentation 


Drupal 8 Resource Guide

Additional Resources:

Drupal 8: Site Install, Module Installation, Directory Layout

Drupal 8: Creating a Blog in Drupal 8 and Configuration API  


comments powered by Disqus