CRUD in computer programming refers to the functions required to perform different operations on specific data within a database. CRUD stands for Create, Read, Update, and Delete. CRUD operations are widely used in applications supported by relational databases.
In this blog, we will look at how to perform CRUD operations utilizing a simple JavaScript front-end system and the Drupal core’s JSON API module.
What is Decoupled Drupal?
Decoupled Drupal refers to a Drupal architecture where Drupal’s back-end exposes content to other front-end systems, essentially serving as a central repository of content that can be served to a wide variety of devices.
What are the advantages of Decoupled Drupal?
A decoupled Drupal architecture enables the development team to take advantage of Drupal’s highly regarded back-end functionality, using Drupal as a repository for content that can be made available to other front-end systems.
Drupal’s JSON API
Drupal Core provides a module named JSON API which provides us with a REST API. This provided API is centered on Drupal’s entity types and bundles. It simply means that the JSON API module converts all the entity types, bundles, etc into a REST API representation. For example, articles, pages, and users are given the types: node--article, node--pages, and user--user, respectively.
To learn more about the JSON API module, click here.
Objective
To understand how CRUD operations are done using the JSON API module used in a Decoupled Drupal Application.
Pre-requisites
Make sure you have a freshly installed Drupal site up and running. That's it!
Authentication using JSON API
Enabling the JSON API module
Using the JSON API module
Creating a Client Secret
Before we move forward, we need a client_id and client_secret, as these terms will be necessary to generate an authorized access token. There is a contributed module in Drupal for generating a client_id and client_secret, the simple OAuth module.
Using the Simple OAuth module :
Download the module
Enable the module
Create the client secret
To create a client secret, follow the below-mentioned steps :
CRUD operations in Decoupled Drupal
Create Operation
First, generate the access token (JWT) for a user.
Making the request to Drupal.
Headers -
Read Operation
Update Operation
Delete Operation
CRUD operations are crucial and frequently used in database and database design cases. The above example explains how we can perform CRUD operations on a Drupal Back-end using the JSON API. While there are many other examples to achieve the same, the premise and ideology remain constant.