diff --git a/packages/volto/news/6236.bugfix b/packages/volto/news/6236.bugfix new file mode 100644 index 0000000000..71ad676eed --- /dev/null +++ b/packages/volto/news/6236.bugfix @@ -0,0 +1 @@ +When there is a contextualized vocabulary url in the schema, Volto must call it with the full path and not return it to the site root. @mamico diff --git a/packages/volto/src/actions/vocabularies/vocabularies.js b/packages/volto/src/actions/vocabularies/vocabularies.js index 811aee2d35..b1107a6b63 100644 --- a/packages/volto/src/actions/vocabularies/vocabularies.js +++ b/packages/volto/src/actions/vocabularies/vocabularies.js @@ -7,6 +7,8 @@ import { GET_VOCABULARY, GET_VOCABULARY_TOKEN_TITLE, } from '@plone/volto/constants/ActionTypes'; +import config from '@plone/registry'; +import { flattenToAppURL } from '@plone/volto/helpers'; import { getVocabName } from '@plone/volto/helpers/Vocabularies/Vocabularies'; import qs from 'query-string'; @@ -28,6 +30,13 @@ export function getVocabulary({ subrequest, }) { const vocabulary = getVocabName(vocabNameOrURL); + const contextualVocabularies = config.settings.contextualVocabularies; + const vocabPath = + contextualVocabularies && + contextualVocabularies.includes(vocabulary) && + vocabulary !== vocabNameOrURL + ? flattenToAppURL(vocabNameOrURL) + : `/@vocabularies/${vocabulary}`; let queryString = `b_start=${start}${size ? '&b_size=' + size : ''}`; @@ -40,7 +49,7 @@ export function getVocabulary({ start, request: { op: 'get', - path: `/@vocabularies/${vocabulary}?${queryString}`, + path: `${vocabPath}?${queryString}`, }, subrequest, }; @@ -62,6 +71,13 @@ export function getVocabularyTokenTitle({ }) { // In case we have a URL, we have to get the vocabulary name const vocabulary = getVocabName(vocabNameOrURL); + const contextualVocabularies = config.settings.contextualVocabularies; + const vocabPath = + contextualVocabularies && + contextualVocabularies.includes(vocabulary) && + vocabulary !== vocabNameOrURL + ? flattenToAppURL(vocabNameOrURL) + : `/@vocabularies/${vocabulary}`; const queryString = { ...(token && { token }), ...(tokens && { tokens }), @@ -75,12 +91,9 @@ export function getVocabularyTokenTitle({ subrequest, request: { op: 'get', - path: `/@vocabularies/${vocabulary}?b_size=-1&${qs.stringify( - queryString, - { - encode: false, - }, - )}`, + path: `${vocabPath}?b_size=-1&${qs.stringify(queryString, { + encode: false, + })}`, }, }; } diff --git a/packages/volto/src/actions/vocabularies/vocabularies.test.js b/packages/volto/src/actions/vocabularies/vocabularies.test.js index b7deafc70e..2ea7b507c2 100644 --- a/packages/volto/src/actions/vocabularies/vocabularies.test.js +++ b/packages/volto/src/actions/vocabularies/vocabularies.test.js @@ -1,3 +1,4 @@ +import config from '../../registry'; import { getVocabulary } from './vocabularies'; import { GET_VOCABULARY } from '@plone/volto/constants/ActionTypes'; @@ -53,5 +54,36 @@ describe('Vocabularies actions', () => { `/@vocabularies/plone.app.vocabularies.Keywords?b_start=0&b_size=-1`, ); }); + it('should create an action to get a contextual vocabulary if a URL with path is passed', () => { + const vocabNameOrURL = + 'http://localhost:8080/de/foo/bar/@vocabularies/plone.app.vocabularies.Keywords'; + const query = 'john'; + config.settings.contextualVocabularies = [ + 'plone.app.vocabularies.Keywords', + ]; + const action = getVocabulary({ vocabNameOrURL, query }); + + expect(action.type).toEqual(GET_VOCABULARY); + expect(action.vocabulary).toEqual(vocabNameOrURL); + expect(action.request.op).toEqual('get'); + expect(action.request.path).toEqual( + `http://localhost:8080/de/foo/bar/@vocabularies/plone.app.vocabularies.Keywords?b_start=0&title=${query}`, + ); + }); + it('could not create an action to get a contextual vocabulary if a vocab name is passed', () => { + const vocabNameOrURL = 'plone.app.vocabularies.Keywords'; + const query = 'john'; + config.settings.contextualVocabularies = [ + 'plone.app.vocabularies.Keywords', + ]; + const action = getVocabulary({ vocabNameOrURL, query }); + + expect(action.type).toEqual(GET_VOCABULARY); + expect(action.vocabulary).toEqual(vocabNameOrURL); + expect(action.request.op).toEqual('get'); + expect(action.request.path).toEqual( + `/@vocabularies/plone.app.vocabularies.Keywords?b_start=0&title=${query}`, + ); + }); }); }); diff --git a/packages/volto/src/config/index.js b/packages/volto/src/config/index.js index 803048b14d..092b4068bf 100644 --- a/packages/volto/src/config/index.js +++ b/packages/volto/src/config/index.js @@ -155,6 +155,7 @@ let config = { showTags: true, controlpanels: [], controlPanelsIcons, + contextualVocabularies: [], filterControlPanels, filterControlPanelsSchema, unwantedControlPanelsFields,