Instantiates a new NextDrupalPages.
const client = new NextDrupalPages(baseUrl)
The baseUrl of your Drupal site. Do not add the /jsonapi suffix.
Options for the client. See Experiment_DrupalClientOptions.
Optional
accessOptional
cacheOptional
fetcherOptional
init: RequestInitGets static paths from the context.
The resource types. Example: node--article
or ["taxonomy_term--tags", "user--user"]
.
The context from getStaticPaths
.
Optional
options: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOptionOptions for the request.
The static paths.
Return static paths for node--page
resources
export async function getStaticPaths(context) {
return {
paths: await drupal.getStaticPathsFromContext("node--page", context),
fallback: "blocking",
}
}
Return static paths for node--page
and node--article
resources
export async function getStaticPaths(context) {
return {
paths: await drupal.getStaticPathsFromContext(
["node--page", "node--article"],
context
),
fallback: "blocking",
}
}
Adds a locale prefix to the given path.
The path.
The options for adding the locale prefix.
Optional
defaultLocale?: stringThe default locale.
Optional
locale?: stringThe locale.
The path with the locale prefix.
Builds an endpoint URL for the specified parameters.
The parameters for the endpoint.
The built endpoint URL.
Builds static paths from resources.
The resources.
Optional
options: { locale?: string; pathPrefix?: string }Options for the request.
The built static paths.
Builds static paths parameters from paths.
The paths.
Optional
options: { locale?: string; pathPrefix?: string }Options for the request.
The built static paths parameters.
Builds a URL with the given path and search parameters.
The path for the url. Example: "/example"
Optional
searchParams: EndpointSearchParamsOptional query parameters.
The constructed URL.
const drupal = new DrupalClient("https://example.com")
// https://drupal.org
drupal.buildUrl("https://drupal.org").toString()
// https://example.com/foo
drupal.buildUrl("/foo").toString()
// https://example.com/foo?bar=baz
client.buildUrl("/foo", { bar: "baz" }).toString()
Build a URL from DrupalJsonApiParams
const params = {
getQueryObject: () => ({
sort: "-created",
"fields[node--article]": "title,path",
}),
}
// https://example.com/jsonapi/node/article?sort=-created&fields%5Bnode--article%5D=title%2Cpath
drupal.buildUrl("/jsonapi/node/article", params).toString()
Constructs a path from the given segment and options.
The path segment.
The options for constructing the path.
Optional
defaultLocale?: stringThe default locale.
Optional
locale?: stringThe locale.
Optional
pathPrefix?: stringThe path prefix.
The constructed path.
Creates a new file resource for the specified media type.
The type of the resource. In most cases this is file--file
.
The body payload with data.
media--image
field_media_image
avatar.jpg
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The created file resource.
Create a file resource for a media--image entity
const file = await drupal.createFileResource("file--file", {
data: {
attributes: {
type: "media--image", // The type of the parent resource
field: "field_media_image", // The name of the field on the parent resource
filename: "filename.jpg",
file: await fs.readFile("/path/to/file.jpg"),
},
},
})
You can then use this to create a new media--image with a relationship to the file:
const media = await drupal.createResource<DrupalMedia>("media--image", {
data: {
attributes: {
name: "Name for the media",
},
relationships: {
field_media_image: {
data: {
type: "file--file",
id: file.id,
},
},
},
},
})
Creates a new resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The body payload with data.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The created resource.
Create a node--page resource
const page = await drupal.createResource("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})
Create a node--article with a taxonomy term
const article = await drupal.createResource("node--article", {
data: {
attributes: {
title: "Title of Article",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
relationships: {
field_category: {
data: {
type: "taxonomy_term--category",
id: "28ab9f26-927d-4e33-9510-b59a7ccdafe6",
},
},
},
},
})
Using filters
const page = await drupal.createResource(
"node--page",
{
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
},
{
params: {
"fields[node--page]": "title,path",
},
}
)
Using TypeScript with DrupalNode
import { DrupalNode } from "next-drupal"
const page = await drupal.createResource<DrupalNode>("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})
Deletes an existing resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The resource id. Example: a50ffee7-ba94-46c9-9705-f9f8f440db94
.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
True if the resource was deleted, false otherwise.
Fetches a resource from the given input URL or path.
The url to fetch from.
The fetch options with withAuth
.
If withAuth
is set, fetch
will fetch an Authorization
header before making the request.
The fetch response.
Retrieve an access token.
Optional
clientIdSecret: NextDrupalAuthClientIdSecretThe client ID and secret.
The access token.
Gets the authentication configuration from the context and options.
The static props context.
Options for the request.
The authentication configuration.
Gets the authorization header value based on the provided auth configuration.
The auth configuration.
The authorization header value.
Get the JSON:API entry for a resource type.
The resource type. Example: node--article
.
Optional
locale: stringOptional. The locale to fetch the index. Example: es
or fr
.
The entry point URL.
By default, when retrieving resources in getResource
or getResourceCollection
,
the DrupalClient
make a request to Drupal to fetch the JSON:API resource entry.
Example: if you provide node--article
, DrupalClient
will make a request to
http://example.com/jsonapi/node/article
.
If you would like to infer the entry from the resource type, use the useDefaultResourceTypeEntry option:
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
useDefaultResourceTypeEntry: true,
})
Extracts errors from the fetch response.
The fetch response.
The extracted errors.
Fetches the JSON:API index.
Optional
locale: stringThe locale for the request.
Optional
options: JsonApiWithNextFetchOptionsOptions for the request.
The JSON:API index.
Fetches a menu by its name.
The name of the menu. Example: main
or footer
.
Optional
options: JsonApiOptions & JsonApiWithCacheOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched menu.
DrupalMenuLinkContent
DrupalMenuLinkContent
with children nested to match the hierarchy from DrupalReturn the path (slug) from getStaticProps or getServerSideProps context.
The context from getStaticProps
or getServerSideProps
.
Optional
options: { pathPrefix?: string }Options for the request.
The constructed path.
Fetches a resource of the specified type by its UUID.
The resource type. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The id of the resource. Example: 15486935-24bf-4be7-b858-a5b2de78d09d
.
Optional
options: JsonApiOptions & JsonApiWithCacheOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched resource.
Get a page by uuid.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)
Get the es translation for a page by uuid.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{
locale: "es",
defaultLocale: "en",
}
)
Get the raw JSON:API response.
const { data, meta, links } = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{
deserialize: false,
}
)
Get a node--article resource using cache.
const id = "07464e9f-9221-4a4f-b7f2-01389408e6c8"
const article = await drupal.getResource("node--article", id, {
withCache: true,
cacheKey: `node--article:${id}`,
})
Get a page resource with time-based revalidation.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{ next: { revalidate: 3600 } }
)
Get a page resource with tag-based revalidation.
const {slug} = params;
const path = drupal.translatePath(slug)
const type = path.jsonapi.resourceName
const tag = `${path.entity.type}:${path.entity.id}`
const node = await drupal.getResource(path, path.entity.uuid, {
params: params.getQueryObject(),
tags: [tag]
})
Using DrupalNode for a node entity type.
import { DrupalNode } from "next-drupal"
const node = await drupal.getResource<DrupalNode>(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)
Using DrupalTaxonomyTerm for a taxonomy term entity type.
import { DrupalTaxonomyTerm } from "next-drupal"
const term = await drupal.getResource<DrupalTaxonomyTerm>(
"taxonomy_term--tags",
"7b47d7cc-9b1b-4867-a909-75dc1d61dfd3"
)
Fetches a resource of the specified type by its path.
The path of the resource. Example: /blog/slug-for-article
.
Optional
options: { isVersionable?: boolean } & (JsonApiOptions & JsonApiWithNextFetchOptions)Options for the request.
The fetched resource.
Get a page by path
const node = await drupal.getResourceByPath("/blog/slug-for-article")
Get the raw JSON:API response
const { data, meta, links } = await drupal.getResourceByPath(
"/blog/slug-for-article",
{
deserialize: false,
}
)
Using DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const node = await drupal.getResourceByPath<DrupalNode>(
"/blog/slug-for-article"
)
Fetches a collection of resources of the specified type.
The type of the resources. Example: node--article
or user--user
.
Optional
options: { deserialize?: boolean } & (JsonApiOptions & JsonApiWithNextFetchOptions)Options for the request.
The fetched collection of resources.
Get all articles
const articles = await drupal.getResourceCollection("node--article")
Using filters
const publishedArticles = await drupal.getResourceCollection("node--article", {
params: {
"filter[status]": "1",
},
})
Get the raw JSON:API response
const { data, meta, links } = await drupal.getResourceCollection("node--page", {
deserialize: false,
})
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const nodes = await drupal.getResourceCollection<DrupalNode[]>("node--article")
Gets a collection of resources from the context.
The type of the resources. Example: node--article
or user--user
.
The static props context from getStaticProps or getServerSideProps.
Optional
options: { deserialize?: boolean } & JsonApiOptionsOptions for the request.
The fetched collection of resources.
The localized resources will be fetched based on the locale
and defaultLocale
values from context
.
Get all articles from context
export async function getStaticProps(context) {
const articles = await drupal.getResourceCollectionFromContext(
"node--article",
context
)
return {
props: {
articles,
},
}
}
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const nodes = await drupal.getResourceCollectionFromContext<DrupalNode[]>(
"node--article",
context
)
Fetches path segments for a collection of resources of the specified types.
The types of the resources.
Optional
options: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption & (Options for the request.
The fetched path segments.
Gets a resource from the context.
Either a resource type (e.g. "node--article") or a translated path from translatePath().
The Next.js context from getStaticProps.
Optional
options: { isVersionable?: boolean; pathPrefix?: string } & JsonApiOptionsOptions for the request.
Optional
isVersionable?: booleanWhether the resource is versionable (defaults to false for all entity types except nodes).
Optional
pathPrefix?: stringThe path prefix to use for the request (defaults to "/").
The fetched resource.
The localized resource will be fetched based on the locale
and defaultLocale
values from context
.
If you pass in a DrupalTranslatedPath
for input, getResourceFromContext
will take the type
and id
from the path and make a getResource
call to Drupal:
export async function getStaticProps(context) {
const path = await drupal.translatePathFromContext(context)
const node = await drupal.getResourceFromContext(path, context)
return {
props: {
node,
},
}
}
If you pass in a string
input, such as node--article
, getResourceFromContext
will make a subrequest call to Drupal to translate the path and then fetch the resource.
You will need both the Subrequests and Decoupled Router modules:
export async function getStaticProps(context) {
const node = await drupal.getResourceFromContext("node--article", context)
return {
props: {
node,
},
}
}
Fetch a resource from context.
export async function getStaticProps(context) {
const node = await drupal.getResourceFromContext("node--page", context)
return {
props: {
node,
},
}
}
Fetch a resource from context in a sub directory.
export async function getStaticProps(context) {
const node = await drupal.getResourceFromContext("node--page", context, {
pathPrefix: "/articles",
})
return {
props: {
node,
},
}
}
Using DrupalNode type:
import { DrupalNode } from "next-drupal"
const node = await drupal.getResourceFromContext<DrupalNode>(
"node--page",
context
)
Using DrupalTaxonomyTerm type:
import { DrupalTaxonomyTerm } from "next-drupal"
const term = await drupal.getResourceFromContext<DrupalTaxonomyTerm>(
"taxonomy_term--tags",
context
)
https://next-drupal.org/docs/typescript for more built-in types.
Fetches a search index by its name.
The name of the search index.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched search index.
Gets a search index from the context.
The name of the search index.
The static props context.
Optional
options: JsonApiOptionsOptions for the request.
The fetched search index.
Gets static paths from the context.
The resource types. Example: node--article
or ["taxonomy_term--tags", "user--user"]
.
The context from getStaticPaths
.
Optional
options: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOptionOptions for the request.
The static paths.
Return static paths for node--page
resources
export async function getStaticPaths(context) {
return {
paths: await drupal.getStaticPathsFromContext("node--page", context),
fallback: "blocking",
}
}
Return static paths for node--page
and node--article
resources
export async function getStaticPaths(context) {
return {
paths: await drupal.getStaticPathsFromContext(
["node--page", "node--article"],
context
),
fallback: "blocking",
}
}
Fetches a view by its name.
The name of the view and the display id. Example: articles--promoted
.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched view.
Get a view named articles
and display id promoted
const view = await drupal.getView("articles--promoted")
Using sparse fieldsets to only fetch the title and body fields
const view = await drupal.getView("articles--promoted", {
params: {
fields: {
"node--article": "title,body",
},
},
})
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const view = await drupal.getView<DrupalNode>("articles--promoted")
Handle preview mode for resources.
The request
from an API route.
The response
from an API route.
Optional
options: { enable: boolean }Options for the request.
Translates a path to a DrupalTranslatedPath object.
The resource path. Example: /blog/slug-for-article
.
Optional
options: JsonApiWithAuthOption & JsonApiWithNextFetchOptionsOptions for the request.
The translated path.
Translates a path from the context.
The context from getStaticProps
or getServerSideProps
.
Optional
options: { pathPrefix?: string } & JsonApiWithAuthOptionOptions for the request.
The translated path.
Updates an existing resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The resource id. Example: a50ffee7-ba94-46c9-9705-f9f8f440db94
.
The body payload with data.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The updated resource.
Update a node--page resource
const page = await drupal.updateResource(
"node--page",
"a50ffee7-ba94-46c9-9705-f9f8f440db94",
{
data: {
attributes: {
title: "Updated Title",
},
},
}
)
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const page = await drupal.updateResource<DrupalNode>(
"node--page",
"a50ffee7-ba94-46c9-9705-f9f8f440db94",
{
data: {
attributes: {
title: "Updated Title",
},
},
}
)
The NextDrupalPages class extends the NextDrupal class and provides methods for interacting with a Drupal backend in the context of Next.js pages.