getMenu
Fetch a menu from Drupal.
⚠️
You need to install the JSON:API Menu Items module to use getMenu
.
const resource = await drupal.getMenu<T = DrupalMenuItem>( menuName, options?: { params, withAuth, deserialize, locale, defaultLocale, withCache, cacheKey, }): Promise<{ items: T[] tree: T[]}>
menuName: string
- Required
- The name of the menu. Example:
main
orfooter
.
options
- Optional
params: JsonApiParams
: JSON:API params such asfilter
,fields
,include
orsort
.withAuth: boolean | NextDrupalAuth
:- 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 ofDrupalMenuItem
.tree
: An array ofDrupalMenuItem
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",})