Drupal : Powerful Core for Decoupled Applications

Technology
30.08.2024
Drupal : Powerful Core for Decoupled Applications
Heena
Heena
Fullstack Developer

Core Modules for Web Services in Drupal

Drupal core includes the following essential modules for web services:

  • HTTP Basic Authentication: Allows for basic authentication of API requests.
  • JSON: Provides a standardised API to expose content and configurations.
  • RESTful Web Services: Enables RESTful APIs for content interaction.
  • Serialisation: Handles the serialisation of data into formats like JSON and XML.

 

Web services

 

Exploring the JSON:API

JSON:API module stands out as a powerful tool for creating a JSON-based API that adheres to the JSON specification. It simplifies the process of exposing Drupal content entities (such as nodes, users, taxonomy terms, etc.) to be consumed by front-end frameworks or other external systems.

Key Features of the JSON:API Module:

  • Standardised API: The module follows a well-defined specification, ensuring consistency and compatibility with various clients.
  • CRUD Operations: The module supports Create, Read, Update, and Delete operations on content entities via HTTP requests.
  • Entity References: One of the most powerful features is the ability to include referenced entities within the same API call, reducing the number of requests needed to retrieve related data.
  • Extensibility: The module can be extended with contributed modules like JSON Extras, allowing for customized API responses and additional functionalities.
     

Practical Examples

To demonstrate the power of the JSON module, let's dive into some practical examples. 
 

  • Basic Content Retrieval

     

    URL Structure

    {{base_url}}/jsonapi/node/{{type}}

     

    Components Explained

    The Drupal site is hosted at https://example.com, then {{base_url}} would be https://example.com.

    /jsonapi: This is the entry point for all JSON requests in Drupal. It signals that you're making a request to retrieve or interact with data in a structured JSON format.

    /node: This part of the URL indicates that you're interacting with content entities of the type "node". In Drupal, a "node" is a content item, like an article, page, or any other content type you have defined.

    /{{type}}: This is a placeholder for the specific content type you want to retrieve.

     

    Example in Practice

    {{base_url}}/jsonapi/node/news

     

get news

 

  • Including Referenced Entities

     

    URL Structure

    Check reference at : https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/fetching-resources-get#s-get-article-media-entity-reference-field-image-url-uri-by-including-references

     

    Components Explained

    It fetches only one news node (page[limit]=1).

    It limits the fields returned for the "news" nodes to just the title and field_multimedia fields. Where multimedia field is a reference to Media (Image).

    It further drills down into related media and file entities to retrieve only the field_media_imageuri, and url fields.

    It includes the related field_multimedia.field_media_image data in the response, so you get all necessary related data in a single request.

     

    Example in Practice

    {{base_url}}/jsonapi/node/news?page[limit]=1&fields[node--news]=title,field_multimedia&fields[media--image]=field_media_image&fields[file-file]=uri,url&include=field_multimedia.field_media_image
     

referenced content

 

References :