Skip to content

Commit 2c07475

Browse files
authored
improve: LDP-2068: Re-add support for useLocalizedMenuEndpoint option (#139)
* improve: LDP-2068: Reimplement useLocalizedMenuEndpoint flag handling. * docs: LDP-2068: Add useLocalizedMenuEndpoint to the list of config options. * improve: LDP-2068: Refetch menus if locale changes.
1 parent ac4e8a4 commit 2c07475

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ The module defaults work well with [Lupus Decoupled Drupal](https://www.drupal.o
7676
while keeping the right status code. By enabling customErrorPages, the regular Nuxt error
7777
pages are shown instead, such that the pages can be customized with Nuxt. Defaults to `false`.
7878

79+
- `useLocalizedMenuEndpoint`: If enabled, the menu endpoint will use a language prefix as configured by [nuxtjs/i18n](https://v8.i18n.nuxtjs.org) module. Defaults to `true`.
80+
7981

8082
## TODO - List of 1.x options not yet implemented
8183

8284
- `addRequestFormat`: If set to `true`, the `_format=custom_elements` URL parameter
8385
is added automatically to requests. Defaults to `true`.
8486

85-
- `useLocalizedMenuEndpoint`: If enabled, the menu endpoint will use a language prefix as configured by [nuxtjs/i18n](https://i18n.nuxtjs.org) module. Defaults to `true`.
86-
8787
- `pageErrorHandler`: The default page error handler can be overridden.
8888

8989
- `menuErrorHandler`: The default menu error handler can be overridden.

src/module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ModuleOptions {
1010
customErrorPages: boolean,
1111
fetchOptions: UseFetchOptions<any>,
1212
fetchProxyHeaders: string[],
13+
useLocalizedMenuEndpoint: boolean,
1314
}
1415

1516
export default defineNuxtModule<ModuleOptions>({
@@ -27,7 +28,8 @@ export default defineNuxtModule<ModuleOptions>({
2728
fetchOptions: {
2829
credentials: 'include',
2930
},
30-
fetchProxyHeaders: ['cookie']
31+
fetchProxyHeaders: ['cookie'],
32+
useLocalizedMenuEndpoint: true
3133
},
3234
setup (options, nuxt) {
3335
const { resolve } = createResolver(import.meta.url)
@@ -46,4 +48,4 @@ declare module 'nuxt/schema' {
4648
interface PublicRuntimeConfig {
4749
drupalCe: ModuleOptions,
4850
}
49-
}
51+
}

src/runtime/composables/useDrupalCe.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { callWithNuxt } from '#app'
22
import { defu } from 'defu'
3-
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, useRoute, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions } from '#imports'
3+
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports'
44

55
export const useDrupalCe = () => {
66

@@ -71,10 +71,21 @@ export const useDrupalCe = () => {
7171
* @param useFetchOptions Optional Nuxt useFetch options
7272
*/
7373
const fetchMenu = async (name: string, useFetchOptions:UseFetchOptions<any> = {}) => {
74-
const menuPath = config.menuEndpoint.replace('$$$NAME$$$', name)
74+
const nuxtApp = useNuxtApp()
7575
useFetchOptions = processFetchOptions(useFetchOptions)
7676
useFetchOptions.key = `menu-${name}`
7777

78+
const baseMenuPath = config.menuEndpoint.replace('$$$NAME$$$', name)
79+
const menuPath = ref(baseMenuPath)
80+
81+
if (config.useLocalizedMenuEndpoint && nuxtApp.$i18n) {
82+
// API path with localization
83+
menuPath.value = nuxtApp.$localePath('/' + baseMenuPath)
84+
watch(nuxtApp.$i18n.locale, () => {
85+
menuPath.value = nuxtApp.$localePath('/' + baseMenuPath)
86+
})
87+
}
88+
7889
const { data: menu, error } = await useFetch(menuPath, useFetchOptions)
7990

8091
if (error.value) {

0 commit comments

Comments
 (0)