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 or footer.
  • options
    • Optional
    • params: JsonApiParams: JSON:API params such as filter, fields, include or sort.
    • 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: Set withCache if you want to store and retrieve the menu from cache.
    • cacheKey: string: The cache key to use.

Notes

  • getMenu returns:
    • items: An array of DrupalMenuItem.
    • tree: An array of DrupalMenuItem 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",
})