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

fix piecesFilters with dynamic choices #4731

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

* 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.
* 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)

Expand Down
18 changes: 18 additions & 0 deletions modules/@apostrophecms/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,24 @@ module.exports = {
});
},

async getChoicesForQueryBuilder(field, query) {
const req = self.apos.task.getReq();
const allChoices = await self.getChoices(req, field);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.getChoices was already there and previously added in #4078, but never used in the builders

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;
Expand Down
29 changes: 2 additions & 27 deletions modules/@apostrophecms/schema/lib/addFieldTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
},
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
Loading