Skip to main content

Adding icon and type elements to the JSON output for React frontend

Working on a decoupled React / Drupal 9 site.

Aim: Adjust the output of curated content for the Work Resources page.  So the icon is displayed with a path.  Enabling React developers to select whether to use the image or icon.

Tools

Drupal 9.4
React  

 

How to get the node.nid from the alias

In this instance, I'll check the beginning of the string.  I want to focus on the href's that start with /node/.  Which has been added using the variable $catchPhrase.

Using the Drupal service path_alias to return the node.nid as follows:

Recently viewed pages

This code is from Drupal 9 back-end for a React front-end via REST API.

Working through the output for recently viewed pages... the code structure:

How to use the Poll module and passing via RESTful API

 

Using the Drupal Poll module and passing via RESTful API -  How do you get it working?

 

The initial set-up

POST: {domain}/mhc_custom/poll?_format=json
Content-type: application/json
Accept: application/json

{
  "choice": "1"
}

 

If you run the above credentials, you will find the following response

How to recover or change or reset your password using REST

How to use the Drupal REST API to trigger I've forgot (need to change) my password.

POST: {domain}/user/password?_format=hal_json
Content-type: application/hal_json
Accept: application/hal+json

Or if you are using hal_json then use

POST: {domain}/user/password?_format=json
Content-type: application/json
Accept: application/json

Body

Drupal REST how to get an image

In retrieving an image, the primary element required is:
  • File id or fid;
 

Query string

Using the element outlined above the query string becomes:

{domain}/file/{fid}/?_format=hal_json

Replacing the following variables:

Drupal how to use node GET command

For an introduction into setting up RESTful hal+json refer to the set-up documentation.

GET a node

To get the content of the specific node you require the node.nid.  The key information required in the GET query:

Drupal how to use node POST command

For an introduction into setting up RESTful hal+json refer to the set-up documentation.

POST command

Following on from the GET command, to post a node to the site (currently staging) is quite quick.  To do so, complete the following:

Request: {domain}/entity/node

Content-Type: application/hal+json

Node PATCH command

For an introduction into setting up RESTful hal+json refer to the set-up documentation.

PATCH command

Following on from the GET command, to patch a node to the site (currently staging) is quite quick.  To be able to PATCH an article, the member needs to have authority to do so.  All members can update their own content.  However, they cannot update any one else's content.

Outline of the details required to make a PATCH:

Subscribe to Drupal