From 40b31da75cdfc21899c0d608e6890ff5953ebff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Herbaux?= Date: Tue, 8 Dec 2020 16:55:27 +0100 Subject: [PATCH] Relational fields - Get mainField from configuration's metadata (#8789) * Returns the mainField for the list view Signed-off-by: Convly * Use lodash fp Signed-off-by: Convly * Move helpers functions outside of the controller Signed-off-by: Convly --- .../controllers/content-types.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/strapi-plugin-content-manager/controllers/content-types.js b/packages/strapi-plugin-content-manager/controllers/content-types.js index 0c8c149c0e..e821aecd69 100644 --- a/packages/strapi-plugin-content-manager/controllers/content-types.js +++ b/packages/strapi-plugin-content-manager/controllers/content-types.js @@ -1,8 +1,16 @@ 'use strict'; +const { has, assoc, mapValues, prop } = require('lodash/fp'); const { getService } = require('../utils'); const { createModelConfigurationSchema, validateKind } = require('./validation'); +const hasEditMainField = has('edit.mainField'); +const getEditMainField = prop('edit.mainField'); +const assocListMainField = assoc('list.mainField'); + +const assocMainField = metadata => + hasEditMainField(metadata) ? assocListMainField(getEditMainField(metadata), metadata) : metadata; + module.exports = { async findContentTypes(ctx) { const { kind } = ctx.query; @@ -31,11 +39,17 @@ module.exports = { } const configuration = await contentTypeService.findConfiguration(contentType); + + const confWithUpdatedMetadata = { + ...configuration, + metadatas: mapValues(assocMainField, configuration.metadatas), + }; + const components = await contentTypeService.findComponentsConfigurations(contentType); ctx.body = { data: { - contentType: configuration, + contentType: confWithUpdatedMetadata, components, }, };