mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
Merge pull request #3699 from strapi/feature/group-mainfield-no-id
Set group default mainField not id unless no attributes
This commit is contained in:
commit
58a820e5b7
@ -12,6 +12,8 @@ const isSortable = (schema, name) => {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (schema.modelType === 'group' && name === 'id') return false;
|
||||
|
||||
const attribute = schema.attributes[name];
|
||||
if (NON_SORTABLES.includes(attribute.type)) {
|
||||
return false;
|
||||
|
||||
@ -9,7 +9,7 @@ async function createDefaultConfiguration(model) {
|
||||
const schema = formatContentTypeSchema(model);
|
||||
|
||||
return {
|
||||
settings: await createDefaultSettings(),
|
||||
settings: await createDefaultSettings(schema),
|
||||
metadatas: await createDefaultMetadatas(schema),
|
||||
layouts: await createDefaultLayouts(schema),
|
||||
};
|
||||
|
||||
@ -3,18 +3,32 @@
|
||||
const _ = require('lodash');
|
||||
const { isSortable } = require('./attributes');
|
||||
|
||||
const getDefaultMainField = schema => {
|
||||
if (schema.modelType == 'group') {
|
||||
// find first group attribute that is sortable
|
||||
return (
|
||||
Object.keys(schema.attributes).find(key => isSortable(schema, key)) ||
|
||||
'id'
|
||||
);
|
||||
}
|
||||
|
||||
return 'id';
|
||||
};
|
||||
|
||||
/**
|
||||
* Retunrs a configuration default settings
|
||||
*/
|
||||
async function createDefaultSettings() {
|
||||
async function createDefaultSettings(schema) {
|
||||
const generalSettings = await strapi.plugins[
|
||||
'content-manager'
|
||||
].services.generalsettings.getGeneralSettings();
|
||||
|
||||
let defaultField = getDefaultMainField(schema);
|
||||
|
||||
return {
|
||||
...generalSettings,
|
||||
mainField: 'id',
|
||||
defaultSortBy: 'id',
|
||||
mainField: defaultField,
|
||||
defaultSortBy: defaultField,
|
||||
defaultSortOrder: 'ASC',
|
||||
};
|
||||
}
|
||||
@ -24,13 +38,17 @@ async function createDefaultSettings() {
|
||||
async function syncSettings(configuration, schema) {
|
||||
if (_.isEmpty(configuration.settings)) return createDefaultSettings(schema);
|
||||
|
||||
const { mainField = 'id', defaultSortBy = 'id' } =
|
||||
let defaultField = getDefaultMainField(schema);
|
||||
|
||||
const { mainField = defaultField, defaultSortBy = defaultField } =
|
||||
configuration.settings || {};
|
||||
|
||||
return {
|
||||
...configuration.settings,
|
||||
mainField: isSortable(schema, mainField) ? mainField : 'id',
|
||||
defaultSortBy: isSortable(schema, defaultSortBy) ? defaultSortBy : 'id',
|
||||
mainField: isSortable(schema, mainField) ? mainField : defaultField,
|
||||
defaultSortBy: isSortable(schema, defaultSortBy)
|
||||
? defaultSortBy
|
||||
: defaultField,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user