Post

drupal react cheatsheet

Run in browser console to see list of page content

1
2
3
4
5
6
7
8
9
10
11
const API_ROOT = "/jsonapi/";
const url = `${API_ROOT}node/article?fields[node--article]=id,drupal_internal__nid,title,body&sort=-created&page[limit]=10`;

const headers = new Headers({
  Accept: "application/vnd.api+json",
});

fetch(url, { headers })
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((err) => console.log("There was an error accessing the API", err));

This fetches content from Drupal’s JSON:API. Specifically, it fetches the

  • id,
  • internal node id,
  • title,
  • body

10 article nodes in reverse chronological order based on creation date. Then it parses the response into a JSON object, and then logs that to the console. The content we’re interested in is in the data key.

This post is licensed under CC BY 4.0 by the author.