Skip to main content

Upload an image file using JSON:API

Whilst I had regularly used HAL to upload files through REST API, this is no longer an option given Hal is now deprecated and will be dropped in Drupal 10.  Time to explore JSON:API.

Drupal modules to be installed

You will need to install

  • JSON:API
  • Media

Configuration for JSON:API

Alter the JSON:API to accept all:

Path: Administration > Configuration > Web services > JSON:API

Programmatically get a Drupal taxonomy term tid from it's name

How do you retrieve a taxonomy term tid value from it's name?

In this situation, use the loadByProperties function.  Load the taxonomy term and then reset the array.

Note, reset - to set the internal pointer of an array to its first element.

See as follows:

Drupal - How do I programmatically set contextual filters?

If you are like me, almost every view I have will have this filter.  So I regularly have the need to set a contextual filter programmatically.  However, some won't and will have other contextual filters instead.

You can pass through the $view object the contextual filter params using the setArguments() method.  Say you want to pass three node nids to the view to filter the results contextually:

Drupal 9 adding a reCAPTCHA element to a webform

 

JSON structure

Outlining the structure of the elements common attributes: type, title, name and required;

Text area

{
    "type": "webform_multiple",
    "title": "What's new?",
    "name": "what_s_new_",
    "required": false
}

Text field

An additional attribute for the text field is maxlength

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  

 

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:

Subscribe to 9.x