"The most compelling advantage of API-first Drupal is not the way it negates Drupal's presentation features, it's the way it enables the capabilities of the CMS to power multiple applications and devices.”— Jason Enter, Manager Technical Services, Acquia.

The relationship between content and code is not a healthy one anymore. The consumer-facing presentation layer is not as removed from the content editor layer, as it used to be, mainly because of the new interactive experiences being provided to consumers through apps.

Modern frameworks and programming languages allow for flexibility in the formation of your content management stack. Thus, decoupling content from code has become a common practice in the recent years and Drupal was one of the early adopters of the same. Let's learn more about Decoupled Drupal and Drupal 8 's decoupled architecture.

What is Decoupled Drupal ?

Well, decoupled Drupal does sound "cool" doesn't it? Seems like everybody is talking about it, so it must be a better, more easier solution right? Well, maybe. Offering a wide array of benefits, this strategy, also known as "headless drupal" is used to exploit Drupal solely as a Content Management System without a presentation layer. i.e there is only content and the administrative UI, sans the public facing themes and the other templates, thus exposing an API for other applications.

But perhaps the most important question that you have in mind would be, what are the benefits of the decoupled Drupal architecture?

A headless architecture does a great job in reducing or completely eleminating most of the problems that are a part of the monolithic content management systems.

Simplified upgrades: A decoupled architecture ensures that a CMS upgrade does not affect the presentation layer, thus keeping you from having to set-up again after every CMS upgradation.

Better focus: A huge advantage for developers, with decoupled architecture, they only need to be concerned about setting up their part of the stack. Back-end developers need not worry about compiling CSS and the front-end guys need not scratch their heads about composer installs.

Easy troubleshooting: A headless architecture is structured in such a way that the platform consists of smaller, discrete parts rather than a single giant application. This makes it a lot easier to locate bugs and troubleshoot them.

Improved performance: With both the teams working simultaneously in a decoupled manner, development of new features can happen seamlessly without running into walls. This in turn improves the overall performance of the platform.

Easy troubleshooting: A headless architecture is structured in such a way that the platform consists of smaller, discrete parts rather than a single giant application. This makes it a lot easier to locate bugs and troubleshoot them.

JSON-API

How Important is JSON:API for a Decoupled Drupal Websites?

Powerful architectures, responsive and user-friendly web applications are a result of JavaScript frameworks, that has become quite popular since its introduction in early 2010. And with an increasing number of people opting to use mobile devices to access digital content, building native applications for a website makes a lot of sense.

Realizing this potential, Drupal 8 has added the support for building RESTful APIs into the core. However, this comes with its own drawbacks as the RESTful services of Drupal core does not provide robust solutions out of the box.

A traditional REST API implementation usually come face to face with a certain set of problems:

  • An entire data graph needed to construct the view in the consumer will require too many requests.
  • A significant response bloat returning all the fields and relationships of every entity.
  • Finding content based on the data attributes of the entities is a herculean task.
  • Versioning the API so consumers and back-end implementations can iterate separately.

In order to address these problems, there are several specifications out there, JSON:API being one of the most prominent ones.

What is JSON?

JSON - Java Script Object Notation is one of the most common data format for the exchange of data over web services. Primarily replacing XML, the lightweight and easy to read nature of JSON makes it quite awesome.

Why you should consider implementing JSON:API specifications?

A set of pre-defined standards and conventions that describe how the APIs should be served by the servers and consumed by the clients for exchanging data in JSON format, the JSON:API specifications offer key benefits when implemented:

Efficiency: The JSON:API specifications are designed in such a way that, they keep the number of requests and the size of the data to bare minimum.

Productivity: It so often happens that, as a devloper you are quite confused about which best practices to follow when building an API. On the other hand, by following a set of standards you are out of dilema.

Consistency: The specifications offer a consistent structure and behaviour while being consumed to build applications.

Support: The specifications are widely accepted and the implementations required for client libraries can be found for almost every framework and programming language.

The JSON:API Module

The JSON:API module for Drupal 8 is an implementation of the JSON:API specification. When you install and enable the JSON:API module, a REST API is immediately available for every type within your Drupal implementation. By traversing entity types and bundles such that it can generate URL's at which it can access and manipulate entities, the module achieves this feat.

While the JSON API is quite a complex specification to deal with, the JSON API module does a great job in simplifying the process of consuming it, especially for early decoupled Drupal developers.

However, by following the approach of "less configurations" and production readiness right of the box, the JSON:API module does come with a set of disadvantages of its own.

Support: The JSON:API module is entirely entity-based, which means that it is centered around the Drupal's entity types and bundles. However, the module fails to support or handle custom data.

Task Failure: Since the module is entity-based and cannot handle custom data, it also fails to perform other tasks such as login, creating sessions or logging out a user etc.

Pre-defined Structure: The JSON:API Module presents a pre-defined payload structure that cannot be modified. Once the module is enabled in Drupal, the consistent payload structure cannot be changed or customized with change in vendor requirements

Learning curve: As JSON:API module exposes all the attributes of an entity, it might get complex in nature. With quite a steep learning curve, JSON:API may not be everyone's cup of tea.

JSON API

How to download and install the JSON:API Module?

UPDATE: As of January 2020, JSON:API module is now a part of Drupal Core (much after this blog was published). Hence, installing it does not require the following step.

Having a dependency on the serialization module, it has to be enabled first before downloading and installing JSON:API. This can be done by using any of the following methods:

Using Drush

$ drush en serialization -y

$ drush dl jsonapi && drush en jsonapi -y

Using Drupal Console

$ drupal module:install serialization

$ drupal module:download jsonapi && drupal module:install jsonapi
Contact us

LET'S DISCUSS YOUR IDEAS. 
WE'D LOVE TO HEAR FROM YOU.

CONTACT US