From 0e1cb6fd477e0f0f7f010a2ac857a8ddaaf654a4 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 11 Jul 2025 09:43:40 +0300 Subject: [PATCH] foreign enum option reference --- client/src/views/fields/enum.js | 2 +- client/src/views/fields/foreign-array.js | 39 +------------------- client/src/views/fields/foreign-enum.js | 47 ++++++++++++++++++------ 3 files changed, 38 insertions(+), 50 deletions(-) diff --git a/client/src/views/fields/enum.js b/client/src/views/fields/enum.js index a3698fe6a3..3c3ec117a9 100644 --- a/client/src/views/fields/enum.js +++ b/client/src/views/fields/enum.js @@ -175,7 +175,7 @@ class EnumFieldView extends BaseFieldView { this.styleMap = this.params.style || this.model.getFieldParam(this.name, 'style') || {}; let optionsPath = this.params.optionsPath; - /** @type {?string} */ + /** @type {string|null} */ const optionsReference = this.params.optionsReference; if (!optionsPath && optionsReference) { diff --git a/client/src/views/fields/foreign-array.js b/client/src/views/fields/foreign-array.js index 6bb3009d00..e830c39efe 100644 --- a/client/src/views/fields/foreign-array.js +++ b/client/src/views/fields/foreign-array.js @@ -27,49 +27,14 @@ ************************************************************************/ import ArrayFieldView from 'views/fields/array'; +import ForeignEnumFieldView from 'views/fields/foreign-enum'; class ForeignArrayFieldView extends ArrayFieldView { type = 'foreign' setupOptions() { - this.params.options = []; - - const field = this.params.field; - const link = this.params.link; - - if (!field || !link) { - return; - } - - const scope = this.getMetadata().get(['entityDefs', this.model.entityType, 'links', link, 'entity']); - - if (!scope) { - return; - } - - let { - optionsPath, - translation, - options, - isSorted, - displayAsLabel, - style, - } = this.getMetadata() - .get(['entityDefs', scope, 'fields', field]); - - options = optionsPath ? this.getMetadata().get(optionsPath) : options; - - this.params.options = Espo.Utils.clone(options) || []; - this.params.translation = translation; - this.params.isSorted = isSorted || false; - this.params.displayAsLabel = displayAsLabel || false; - this.styleMap = style || {}; - - this.translatedOptions = Object.fromEntries( - this.params.options - .map(item => [item, this.getLanguage().translateOption(item, field, scope)]) - ); + ForeignEnumFieldView.prototype.setupOptions.call(this); } } diff --git a/client/src/views/fields/foreign-enum.js b/client/src/views/fields/foreign-enum.js index 30ee119f49..741c0a800c 100644 --- a/client/src/views/fields/foreign-enum.js +++ b/client/src/views/fields/foreign-enum.js @@ -42,13 +42,25 @@ class ForeignEnumFieldView extends EnumFieldView { return; } - const scope = this.getMetadata().get(['entityDefs', this.model.entityType, 'links', link, 'entity']); + const entityType = this.getMetadata().get(`entityDefs.${this.model.entityType}.links.${link}.entity`); - if (!scope) { + if (!entityType) { return; } - const fieldDefs = this.getMetadata().get(['entityDefs', scope, 'fields', field]); + /** + * @type {{ + * optionsPath?: string|null, + * optionsReference?: string|null, + * translation?: string|null, + * options?: string[], + * isSorted?: boolean, + * displayAsLabel?: boolean, + * style?: Record, + * labelType?: string, + * }} + */ + const fieldDefs = this.getMetadata().get(`entityDefs.${entityType}.fields.${field}`); if (!fieldDefs) { return; @@ -56,6 +68,7 @@ class ForeignEnumFieldView extends EnumFieldView { let { optionsPath, + optionsReference, translation, options, isSorted, @@ -64,19 +77,29 @@ class ForeignEnumFieldView extends EnumFieldView { labelType, } = fieldDefs; - options = optionsPath ? this.getMetadata().get(optionsPath) : options; + if (!optionsPath && optionsReference) { + const [refEntityType, refField] = optionsReference.split('.'); - this.params.options = Espo.Utils.clone(options) || []; + optionsPath = `entityDefs.${refEntityType}.fields.${refField}.options`; + + style = this.getMetadata().get(`entityDefs.${refEntityType}.fields.${refField}.style`) ?? {}; + } + + if (optionsPath) { + options = this.getMetadata().get(optionsPath); + } + + this.params.options = Espo.Utils.clone(options) ?? []; this.params.translation = translation; - this.params.isSorted = isSorted || false; - this.params.displayAsLabel = displayAsLabel || false; + this.params.isSorted = isSorted ?? false; + this.params.displayAsLabel = displayAsLabel ?? false; this.params.labelType = labelType; - this.styleMap = style || {}; + this.styleMap = style ?? {}; - this.translatedOptions = Object.fromEntries( - this.params.options - .map(item => [item, this.getLanguage().translateOption(item, field, scope)]) - ); + const pairs = this.params.options + .map(item => [item, this.getLanguage().translateOption(item, field, entityType)]) + + this.translatedOptions = Object.fromEntries(pairs); } }