1. Creating a custom API for
a headless Drupal
Alberto G. Viu alberto@exove.fi
2. What is a headless Drupal?
Drupal Back-end
Drupal Head (Theming)
C
L
I
E
N
T
S
E
R
V
E
R
Traditional Drupal stack Runs in Headless Drupal
Drupal Back-end
Client Apps
Drupal API
3. Why headless Drupal?
● Installation is easier to maintain (no rendering to take care of)
● Due to that, performance is much better
● Easier to work with different teams
Because client-side frontend frameworks are the future of the web
Also it comes with some advantages:
Clients that could use the API:
● Other Drupal sites
● Mobile apps
● Flash components
● Other servers and services
● Internet of things
4. How to create the API?
● Drupal community provides four major solutions.
● Views Datasource
● You don't need any code at all
● Configured by UI
● … but very limited.
● RestWS
● REST Web Services
● Contrib in D7, in core in D8
● Wrappers EntityAPI
5. How to create the API? (II)
● Restful
● Newest in town
● Best practices for security, performance and usability
● Extremely developer oriented (site builders, go away)
● Services
● Framework for creating custom API's
● Introduces endpoints, key for creating custom paths
● Ships with basic entities resources created.
● Allows create other server such as SOAP.
● Lots of extensions: views, rules, Search API, Authentication...
6. How to use Services?
● Services module introduces two concepts: endpoints and
resources.
● Endpoints
● Defines a custom path
● Defines authentication mode
● Attach resources to them
● Exportable
● Resources
● CRUD operations
● Fixed path pattern for the CRUD operations
● Actions: Global (e.g. Solr reindex)
● Targeted actions (e.g. publish node)
● Relationships (e.g. retrieve comments from node)
7. Schema of a resource
● CRUD:
● Create: POST /endpoint/resource/ + body data
● Retrieve: GET /endpoint/resource/id
● Update: PUT /endpoint/resource/id + body data
● Delete: DELETE /endpoint/resource/id
● Index: GET /endpoint/resource/
● Actions:
● POST /endpoint/resource/my_custom_action
● Targeted actions:
● POST /endpoint/resource/id/my_targeted_action
●
● Relationships:
● GET /endpoint/resource/id/related_content