Check your version
This collection covers a topic in Drupal 10 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 10, but we consider it archived.
In this series we're going to take a closer look at Decoupled (or Headless) Drupal.
We'll start out by talking about what it really means to decouple your website. Then we'll come up with a list of criteria that should be considered when deciding if pursuing a decoupled approach is a good idea for your project. Then, we'll start in planning our decoupled project by talking about the components that make up a solid API. With the fundamentals in place, we'll then talk about making sure your API is documented and tested.
Next, we'll look at a variety of methods in Drupal we have at our disposal to expose data from our site as an API. We'll take a quick look at writing custom code, using Views Datasource, Services module, and the RESTful module to expose data from a Drupal 7 site. We'll also take a look at what Drupal 8 brings to the table to help us build out a REST API without writing a single line of code.
If you're interested in decoupling Drupal, there's a good chance you've heard at least some of the buzz in the Drupalverse about "headless" or decoupled Drupal. Or perhaps you watched Dries' keynote from DrupalCon Barcelona or read Dries' blog post about the future of decoupled Drupal. Whatever the case may be, this tutorial and the ones that follow will walk you through building a simple decoupled blog. In Dries' terminology the demo site we'll be building is "fully decoupled." While it would be trivial to adopt similar techniques to build a progressively decoupled site, let's dig a bit deeper into what it means to build a decoupled Drupal site.
Andrew Berry, from Lullabot, has written a great article asking Should you Decouple? Like most architectural decisions there are trade-offs to consider with a decoupled approach. Let's take a look at some of the pros and cons of a decoupled approach. Is it the right choice for your project?
There's been a lot written about API design, it's probably not surprising there are several books written about the subject. It also seems like nearly every cloud-based service provides an API to allow access to your data. In this tutorial, we'll attempt to condense this information and answer the following questions:
- Are there different types of API paradigms?
- What kinds of considerations do we need to make when building an API for our decoupled site?
- And, what's this REST thing everyone is talking about?
Let's dig into those questions one at a time.
In this tutorial, I'll introduce the API Blueprint specification and take a look at a few tools we can use to provide documentation and testing for our API.
The tools we'll look at include:
By the end of the tutorial you'll have a better understanding of the API Blueprint specification and be able to use Dredd and Aglio to ensure your API documentation and testing stay up-to-date.
Self-check question: Could you write a script that could be run after every commit that would keep your documentation up-to-date, and provide API test results?
In this tutorial, I'll show you a few ways we can create a basic API by exposing data from our Drupal site.
The methods I'll use include:
- Views Datasource
- Custom code
Self-check question: In what situations would you consider using each of these approaches?
Another popular method of creating an API from a Drupal site is using the RESTful module.
In this exploration of the module we will:
- Walk through the documentation
- Use the example code to expose basic entity information
- Write custom code to expose more complex data
By the end of this lesson we'll have built the final API we're going to use to build a demo application.
Self-check question: Without looking at the repository, can you write the code necessary to expose Drupal menus using the RESTful module?
Core in Drupal 8 comes with a built-in solution for providing creating a REST API without writing a single line of code.
NOTE: We now have a full series on Web Services in Drupal that goes into much more detail about building your API for a decoupled site.
In this lesson we will talk about the core modules that help create a REST API, including:
- REST module
- Basic Authentication
- and the RESTUI contributed module
Self-check question: Can you find examples in Drupal core of plugins that expose data to the REST server?
- The video covers creating a node (via POST) twice, using two different versions of the Postman Chrome extension.
- The REST_UI module has been updated since the creation of this video. Rather than providing unique permissions for users it now leverages entity permissions to determine if a user has access to a particular operation or not.
Self-check challenge: Create another sidebar block in our blog that pulls data from Open Weather Map to display the current temperature in your city.
Building a traditionally "coupled" Drupal site provides us with a lot of functionality we've so far taken for granted when building our simple blog.
This lesson attempts to remedy some of that by: building a functional pager and a post archive.
We'll also make our blog into an actual Single Page Application (or SPA) by using the client-side router provided by Backbone.js
Self-check challenge: Add a form with select list elements that filter posts (instead of relying on the sidebar links).
In particular we'll learn about:
- Writing a server with Express.js
What is an isomorphic application? — by Juampy NR on Lullabot.com