Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contextual vocabularies #6236

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions packages/volto/news/6236.bugfix
Original file line number Diff line number Diff line change
@@ -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
27 changes: 20 additions & 7 deletions packages/volto/src/actions/vocabularies/vocabularies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 : ''}`;

Expand All @@ -40,7 +49,7 @@ export function getVocabulary({
start,
request: {
op: 'get',
path: `/@vocabularies/${vocabulary}?${queryString}`,
path: `${vocabPath}?${queryString}`,
},
subrequest,
};
Expand All @@ -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 }),
Expand All @@ -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,
})}`,
},
};
}
32 changes: 32 additions & 0 deletions packages/volto/src/actions/vocabularies/vocabularies.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from '../../registry';
import { getVocabulary } from './vocabularies';
import { GET_VOCABULARY } from '@plone/volto/constants/ActionTypes';

Expand Down Expand Up @@ -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}`,
);
});
});
});
1 change: 1 addition & 0 deletions packages/volto/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ let config = {
showTags: true,
controlpanels: [],
controlPanelsIcons,
contextualVocabularies: [],
filterControlPanels,
filterControlPanelsSchema,
unwantedControlPanelsFields,
Expand Down
Loading