From 65cfe1dd3bbe5aee0613f93963e0dff0b0dac571 Mon Sep 17 00:00:00 2001 From: Etienne Laurent Date: Wed, 18 Sep 2024 18:08:29 +0200 Subject: [PATCH] fix piecesFilters with dynamic choices --- CHANGELOG.md | 1 + modules/@apostrophecms/schema/index.js | 18 ++++++++++++ .../schema/lib/addFieldTypes.js | 29 ++----------------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 596ef92ca0..347921165a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Uploaded SVGs now permit `` tags granted their `xlink:href` property is a local reference and begins with the `#` character. This improves SVG support while mitgating XSS vulnerabilities. * Default properties of object fields present in a widget now populate correctly even if never focused in the editor. +* Fixed the "choices" query builder to correctly support dynamic choices, ensuring compatibility with the [`piecesFilters`](https://docs.apostrophecms.org/reference/modules/piece-page-type.html#piecesfilters) feature when using dynamic choices. ## 4.7.0 (2024-09-05) diff --git a/modules/@apostrophecms/schema/index.js b/modules/@apostrophecms/schema/index.js index 85ba39c7dc..b9eb774b67 100644 --- a/modules/@apostrophecms/schema/index.js +++ b/modules/@apostrophecms/schema/index.js @@ -1706,6 +1706,24 @@ module.exports = { }); }, + async getChoicesForQueryBuilder(field, query) { + const req = self.apos.task.getReq(); + const allChoices = await self.getChoices(req, field); + const values = await query.toDistinct(field.name); + + const choices = _.map(values, function (value) { + const choice = _.find(allChoices, { value: value }); + return { + value: value, + label: choice && (choice.label || value) + }; + }); + + self.apos.util.insensitiveSortByProperty(choices, 'label'); + + return choices; + }, + async getChoices(req, field) { if (typeof field.choices !== 'string') { return field.choices; diff --git a/modules/@apostrophecms/schema/lib/addFieldTypes.js b/modules/@apostrophecms/schema/lib/addFieldTypes.js index 6eaf2600b1..4133781921 100644 --- a/modules/@apostrophecms/schema/lib/addFieldTypes.js +++ b/modules/@apostrophecms/schema/lib/addFieldTypes.js @@ -339,16 +339,7 @@ module.exports = (self) => { } }, choices: async function () { - const values = await query.toDistinct(field.name); - const choices = _.map(values, function (value) { - const choice = _.find(field.choices, { value: value }); - return { - value: value, - label: choice && (choice.label || value) - }; - }); - self.apos.util.insensitiveSortByProperty(choices, 'label'); - return choices; + return self.getChoicesForQueryBuilder(field, query); } }); }, @@ -408,23 +399,7 @@ module.exports = (self) => { } }, choices: async function () { - let allChoices; - const values = await query.toDistinct(field.name); - if ((typeof field.choices) === 'string') { - const req = self.apos.task.getReq(); - allChoices = await self.apos.modules[field.moduleName][field.choices](req); - } else { - allChoices = field.choices; - } - const choices = _.map(values, function (value) { - const choice = _.find(allChoices, { value: value }); - return { - value: value, - label: choice && (choice.label || value) - }; - }); - self.apos.util.insensitiveSortByProperty(choices, 'label'); - return choices; + return self.getChoicesForQueryBuilder(field, query); } }); }