Skip to content

Commit 3d381a9

Browse files
authored
fix piecesFilters with dynamic choices (#4731)
1 parent 4814713 commit 3d381a9

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* The `@apostrophecms/page` module APIs no longer allow a page to become a child of itself. Thanks to [Maarten Marx](https://github.com/Pixelguymm) for reporting the issue.
1818
* Uploaded SVGs now permit `<use>` tags granted their `xlink:href` property is a local reference and begins with the `#` character. This improves SVG support while mitgating XSS vulnerabilities.
1919
* Default properties of object fields present in a widget now populate correctly even if never focused in the editor.
20+
* 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.
2021

2122
## 4.7.1 (2024-09-20)
2223

modules/@apostrophecms/schema/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,24 @@ module.exports = {
17061706
});
17071707
},
17081708

1709+
async getChoicesForQueryBuilder(field, query) {
1710+
const req = self.apos.task.getReq();
1711+
const allChoices = await self.getChoices(req, field);
1712+
const values = await query.toDistinct(field.name);
1713+
1714+
const choices = _.map(values, function (value) {
1715+
const choice = _.find(allChoices, { value: value });
1716+
return {
1717+
value: value,
1718+
label: choice && (choice.label || value)
1719+
};
1720+
});
1721+
1722+
self.apos.util.insensitiveSortByProperty(choices, 'label');
1723+
1724+
return choices;
1725+
},
1726+
17091727
async getChoices(req, field) {
17101728
if (typeof field.choices !== 'string') {
17111729
return field.choices;

modules/@apostrophecms/schema/lib/addFieldTypes.js

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,7 @@ module.exports = (self) => {
339339
}
340340
},
341341
choices: async function () {
342-
const values = await query.toDistinct(field.name);
343-
const choices = _.map(values, function (value) {
344-
const choice = _.find(field.choices, { value: value });
345-
return {
346-
value: value,
347-
label: choice && (choice.label || value)
348-
};
349-
});
350-
self.apos.util.insensitiveSortByProperty(choices, 'label');
351-
return choices;
342+
return self.getChoicesForQueryBuilder(field, query);
352343
}
353344
});
354345
},
@@ -408,23 +399,7 @@ module.exports = (self) => {
408399
}
409400
},
410401
choices: async function () {
411-
let allChoices;
412-
const values = await query.toDistinct(field.name);
413-
if ((typeof field.choices) === 'string') {
414-
const req = self.apos.task.getReq();
415-
allChoices = await self.apos.modules[field.moduleName][field.choices](req);
416-
} else {
417-
allChoices = field.choices;
418-
}
419-
const choices = _.map(values, function (value) {
420-
const choice = _.find(allChoices, { value: value });
421-
return {
422-
value: value,
423-
label: choice && (choice.label || value)
424-
};
425-
});
426-
self.apos.util.insensitiveSortByProperty(choices, 'label');
427-
return choices;
402+
return self.getChoicesForQueryBuilder(field, query);
428403
}
429404
});
430405
}

0 commit comments

Comments
 (0)