getMenu
Fetch a menu from Drupal.
⚠️
You need to install the JSON:API Menu Items module to use getMenu
.
const resource = await drupal.getResource<T = DrupalMenuLinkContent>( name, options?: { params, withAuth, deserialize, locale, defaultLocale, withCache }): Promise<{ items: T[] tree: T[]}>
name: string
- Required
- The name of the menu. Example:
main
orfooter
.
options
- Optional
params: JsonApiParams
: JSON:API params such asfilter
,fields
,include
orsort
.withAuth: boolean | DrupalClientAuth
:- Set the authentication method to use. See the authentication docs.
- Set to
true
to use the authentication method configured on the client.
deserialize: boolean
: Set to false to return the raw JSON:API response.locale: string
: The locale to fetch the resource in.defaultLocale: string
: The default locale of the site.withCache: boolean
: SetwithCache
if you want to store and retrieve the menu from cache.cacheKey: string
: The cache key to use.
Notes
getMenu
returns:items
: An array ofDrupalMenuLinkContent
.tree
: An array ofDrupalMenuLinkContent
with children nested to match the hierarchy from Drupal.
Examples
- Get the
main
menu.
const { menu, items } = await drupal.getMenu("main")
- Get the
main
menu using cache.
const menu = await drupal.getMenu("main", { withCache: true, cacheKey: "menu--main",})