Skip to content

Commit

Permalink
Check that the custom type bind field in configured in the `submissio…
Browse files Browse the repository at this point in the history
…n-forms` when it is configured in the cfg property (#745)
  • Loading branch information
milanmajchrak authored Nov 28, 2024
1 parent c335482 commit b782d78
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/app/shared/form/builder/form-builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { FormRowModel } from '../../../core/config/models/config-submission-form
/**
* The key for the default type bind field. {'default': 'dc_type'}
*/
export const TYPE_BIND_DEFAULT = 'default';
export const TYPE_BIND_DEFAULT_KEY = 'default';

@Injectable()
export class FormBuilderService extends DynamicFormService {
Expand Down Expand Up @@ -91,7 +91,7 @@ export class FormBuilderService extends DynamicFormService {
this.typeFields = new Map();
this.typeBindModel = new Map();

this.typeFields.set(TYPE_BIND_DEFAULT, 'dc_type');
this.typeFields.set(TYPE_BIND_DEFAULT_KEY, 'dc_type');
// If optional config service was passed, perform an initial set of type field (default dc_type) for type binds
if (hasValue(this.configService)) {
this.setTypeBindFieldFromConfig();
Expand All @@ -118,9 +118,18 @@ export class FormBuilderService extends DynamicFormService {
getTypeBindModel(typeBingField: string): DynamicFormControlModel {
let typeBModelKey = this.typeFields.get(typeBingField);
if (isUndefined(typeBModelKey)) {
typeBModelKey = this.typeFields.get(TYPE_BIND_DEFAULT);
typeBModelKey = this.typeFields.get(TYPE_BIND_DEFAULT_KEY);
}
return this.typeBindModel.get(typeBModelKey);
// 1. The candidate is undefined when the custom field is configured for a specific metadata field
// e.g., `dc.language.iso=>edm.type`, but the custom field is not configured in the
// `submission-forms` configuration (type-bind without custom `field`)
// 2. If candidateTypeBindModel does not have custom `field` property, return the default typeBindModel, also
// that means that the custom field is not configured in the `submission-forms` configuration
const candidateTypeBindModel = this.typeBindModel.get(typeBModelKey);
if (isUndefined(candidateTypeBindModel) || isNotUndefined((candidateTypeBindModel as any).typeBindField)) {
return this.typeBindModel.get(this.typeFields.get(TYPE_BIND_DEFAULT_KEY));
}
return candidateTypeBindModel;
}

setTypeBindModel(model: DynamicFormControlModel) {
Expand Down Expand Up @@ -587,7 +596,7 @@ export class FormBuilderService extends DynamicFormService {
}

// Always update the typeFields map with the default value, normalized
this.typeFields.set(TYPE_BIND_DEFAULT, typeFieldConfigValue.replace(/\./g, '_'));
this.typeFields.set(TYPE_BIND_DEFAULT_KEY, typeFieldConfigValue.replace(/\./g, '_'));
});
});
}
Expand All @@ -599,11 +608,11 @@ export class FormBuilderService extends DynamicFormService {
getTypeField(metadataField: string): string {
if (hasValue(this.configService) && isEmpty(this.typeFields.values())) {
this.setTypeBindFieldFromConfig(metadataField);
} else if (hasNoValue(this.typeFields.get(TYPE_BIND_DEFAULT))) {
this.typeFields.set(TYPE_BIND_DEFAULT, 'dc_type');
} else if (hasNoValue(this.typeFields.get(TYPE_BIND_DEFAULT_KEY))) {
this.typeFields.set(TYPE_BIND_DEFAULT_KEY, 'dc_type');
}

return this.typeFields.get(metadataField) || this.typeFields.get(TYPE_BIND_DEFAULT);
return this.typeFields.get(metadataField) || this.typeFields.get(TYPE_BIND_DEFAULT_KEY);
}

}
3 changes: 3 additions & 0 deletions src/app/shared/form/builder/models/form-field.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ export class FormFieldModel {

@autoserialize
autocompleteCustom: string;

@autoserialize
typeBindField: string;
}
3 changes: 3 additions & 0 deletions src/app/shared/form/builder/parsers/field-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ export abstract class FieldParser {
this.parserOptions.typeField);
}

if (isNotEmpty(this.configData.typeBindField)) {
controlModel.typeBindField = this.configData.typeBindField;
}
return controlModel;
}

Expand Down

0 comments on commit b782d78

Please sign in to comment.