Reference

Server API Reference for next-drupal


The following functions are meant to be called on the server (`getStaticPaths`, `getStaticProps` and `getServerSideProps`).

getPathsFromContext

Returns a list of paths for dynamic routes in `getStaticPaths` or `getServerSideProps`.

`getPathsFromContext` accepts a resource type or an array of resource types.

If you have locales configured in your `next.config.js` file, it will automatically create localized routes.

function getPathsFromContext(
types: string | string[],
context: GetStaticPathsContext,
options?: {
params?: JsonApiParams
}
): Promise<any[]>

Examples

Get paths for all article nodes.

pages/[[...slug]].tsx

import { getPathsFromContext } from "next-drupal"
export async function getStaticPaths(context) {
return {
paths: await getPathsFromContext("node--article", context),
fallback: true,
}
}

Get paths for all published article and page nodes.

pages/[[...slug]].tsx

import { getPathsFromContext } from "next-drupal"
export async function getStaticPaths(context) {
return {
paths: await getPathsFromContext(["node--article", "node--page"], context, {
params: {
"filter[status]": "1",
},
}),
fallback: true,
}
}

getResourceTypeFromContext

Returns a resource type in `getStaticProps` or `getServerSideProps`.

function getResourceTypeFromContext(
context: GetStaticPropsContext,
options?: {
prefix?: string
}
): Promise<string>

Examples

pages/[[...slug]].tsx

import { getResourceTypeFromContext } from "next-drupal"
export async function getStaticProps(context) {
const type = await getResourceTypeFromContext(context)
if (type === "node--article") {
// Do something.
}
}

getResource

Loads a resource by ID (uuid) in `getStaticProps` or `getServerSideProps`.

function getResource(
type: string,
uuid: string,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions
): Promise<any>

Examples

Get a page by uuid.

const node = await getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)

Get the `es` translation for a page by uuid.

const node = await getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{
locale: "es",
defaultLocale: "en",
}
)

getResourceFromContext

Returns a single resource in `getStaticProps` or `getServerSideProps`.

function getResourceFromContext(
type: string,
context: GetStaticPropsContext,
options?: {
prefix?: string
deserialize?: boolean
params?: JsonApiParams
}
): Promise<any>

Examples

Get a page node.

pages/[[...slug]].tsx

import { getResourceFromContext } from "next-drupal"
export async function getStaticProps(context) {
const node = await getResourceFromContext("node--page", context)
return {
props: {
node,
},
revalidate: 60,
}
}

getResourceByPath

Get a resource by path in `getStaticProps` or `getServerSideProps`.

function getResourceByPath(
path: string,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions
): Promise<any>

Examples

Get an article by path.

const node = await getResourceByPath("/blog/hello-world")

Get the `es` translation for an article by path.

const node = await getResourceByPath("/blog/hello-world", {
locale: "es",
defaultLocale: "en",
})

Get some fields for an article by path.

const node = await getResourceByPath("/blog/hello-world", {
params: {
"fields[node--article]": "title,body,status,path",
},
})

getResourceCollection

Returns a collection of resources in `getStaticProps` or `getServerSideProps`.

function getResourceCollection(
type: string,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions
): Promise<any>

Examples

Get a collection of articles.

const articles = await getResourceCollection("node--article")

Get a collection of published articles.

const articles = await getResourceCollection("node--article", {
params: {
"filter[status]": "1",
},
})

getResourceCollectionFromContext

Returns a collection of resources from context in `getStaticProps` or `getServerSideProps`.

function getResourceCollectionFromContext(
type: string,
context: GetStaticPropsContext,
options?: {
deserialize?: boolean
params?: JsonApiParams
}
): Promise<any>

Examples

Get a collection of articles

export async function getStaticProps(context) {
const articles = await getResourceCollectionFromContext(
"node--article",
context
)
}

getMenu

⚠️

You need to install the JSON:API Menu Items module to use `useMenu` and `getMenu`.

Returns menu items for a menu by name in `getStaticProps` or `getServerSideProps`.

function getMenu(
name: string,
options?: {
deserialize?: boolean
} & JsonApiWithLocaleOptions
): Promise<{
items: DrupalMenuLinkContent[]
tree: DrupalMenuLinkContent[]
}>
  • `items` is an array of menu items sorted by weight.
  • `tree` is the hierarchical menu tree with parent and children.

Examples

const { tree, items } = await getMenu("main")

translatePath

Returns the translated path from Decoupled Router.

⚠️

This is available in `next-drupal ^1.1.0`. See a guide on how to use translatedPath to handle redirect here.

function translatePath(
path: string,
options?: {
accessToken?: AccessToken
}
): Promise<DrupalTranslatedPath>

Examples

const path = await translatePath("/about")

translatePathFromContext

Returns the translated path from getStaticProps and getServerSideProps context.

function translatePathFromContext(
context: GetStaticPropsContext,
options?: {
accessToken?: AccessToken
prefix?: string
}
): Promise<DrupalTranslatedPath>

Examples

export async function getStaticProps(context) {
const path = await translatePathFromContext(context)
}

--

getView

⚠️

You need to install the JSON:API Views module to use `getView`.

Returns view results in `getStaticProps` or `getServerSideProps`.

export async function getView<T>(
name: string,
options?: {
deserialize?: boolean
accessToken?: AccessToken
} & JsonApiWithLocaleOptions
): Promise<{
results: T
meta: Record<string, any>
links: {
[key in "next" | "prev" | "self"]?: {
href: "string"
}
}
}>
  • `results` is an array of view results.
  • `meta` has additional information from the view. ex. the count.

Examples

const view = await getView("article--block_1")

You can also pass additional params to `getView`.

// Get the articles--block_1 view with user information.
const view = await getView("article--block_1", {
params: {
include: "uid",
},
})

Filters

You can pass filters to the view just like JSON:API params. Note `status` is an exposed filter on the view.

// Get the articles--block_1 view with user information and filter by status true.
const view = await getView("article--block_1", {
params: {
include: "uid",
"views-filter[status]": 1,
},
})

TypeScript

import { DrupalNode } from "next-drupal"
const view = await getView<DrupalNode[]>("article--block_1")

`view.results` will be properly typed as an array of `DrupalNode`.