mirror of
https://github.com/strapi/strapi.git
synced 2025-12-09 05:50:37 +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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (schema.modelType === 'group' && name === 'id') return false;
|
||||||
|
|
||||||
const attribute = schema.attributes[name];
|
const attribute = schema.attributes[name];
|
||||||
if (NON_SORTABLES.includes(attribute.type)) {
|
if (NON_SORTABLES.includes(attribute.type)) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -9,7 +9,7 @@ async function createDefaultConfiguration(model) {
|
|||||||
const schema = formatContentTypeSchema(model);
|
const schema = formatContentTypeSchema(model);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
settings: await createDefaultSettings(),
|
settings: await createDefaultSettings(schema),
|
||||||
metadatas: await createDefaultMetadatas(schema),
|
metadatas: await createDefaultMetadatas(schema),
|
||||||
layouts: await createDefaultLayouts(schema),
|
layouts: await createDefaultLayouts(schema),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,18 +3,32 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const { isSortable } = require('./attributes');
|
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
|
* Retunrs a configuration default settings
|
||||||
*/
|
*/
|
||||||
async function createDefaultSettings() {
|
async function createDefaultSettings(schema) {
|
||||||
const generalSettings = await strapi.plugins[
|
const generalSettings = await strapi.plugins[
|
||||||
'content-manager'
|
'content-manager'
|
||||||
].services.generalsettings.getGeneralSettings();
|
].services.generalsettings.getGeneralSettings();
|
||||||
|
|
||||||
|
let defaultField = getDefaultMainField(schema);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...generalSettings,
|
...generalSettings,
|
||||||
mainField: 'id',
|
mainField: defaultField,
|
||||||
defaultSortBy: 'id',
|
defaultSortBy: defaultField,
|
||||||
defaultSortOrder: 'ASC',
|
defaultSortOrder: 'ASC',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -24,13 +38,17 @@ async function createDefaultSettings() {
|
|||||||
async function syncSettings(configuration, schema) {
|
async function syncSettings(configuration, schema) {
|
||||||
if (_.isEmpty(configuration.settings)) return createDefaultSettings(schema);
|
if (_.isEmpty(configuration.settings)) return createDefaultSettings(schema);
|
||||||
|
|
||||||
const { mainField = 'id', defaultSortBy = 'id' } =
|
let defaultField = getDefaultMainField(schema);
|
||||||
|
|
||||||
|
const { mainField = defaultField, defaultSortBy = defaultField } =
|
||||||
configuration.settings || {};
|
configuration.settings || {};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...configuration.settings,
|
...configuration.settings,
|
||||||
mainField: isSortable(schema, mainField) ? mainField : 'id',
|
mainField: isSortable(schema, mainField) ? mainField : defaultField,
|
||||||
defaultSortBy: isSortable(schema, defaultSortBy) ? defaultSortBy : 'id',
|
defaultSortBy: isSortable(schema, defaultSortBy)
|
||||||
|
? defaultSortBy
|
||||||
|
: defaultField,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user