Skip to content

Commit

Permalink
SearchKit - Use EntityRef for all FK fields, even those with option l…
Browse files Browse the repository at this point in the history
…ists

Before: option lists were preferred over FK
After: FK is preferred over option lists

Fixes dev/core#3950
Fixes dev/core#3982
  • Loading branch information
colemanw committed Nov 16, 2022
1 parent 53f1708 commit e2f6270
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ang/crmUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@
$timeout(function() {
$element.crmAutocomplete(ctrl.entity, ctrl.crmAutocompleteParams, {
multiple: ctrl.multi,
minimumInputLength: ctrl.autoOpen && !ctrl.staticOptions ? 0 : 1,
minimumInputLength: ctrl.autoOpen && _.isEmpty(ctrl.staticOptions) ? 0 : 1,
static: ctrl.staticOptions || [],
});
});
Expand Down
5 changes: 4 additions & 1 deletion ext/search_kit/ang/crmSearchAdmin.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@
entity.optionsLoaded = false;
entitiesToLoad[entityName] = [entityName, 'getFields', {
loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'],
where: [['options', '!=', false]],
// For fields with both an FK and an option list, prefer the FK
// because it's more efficient to render an autocomplete than to
// pre-load potentially thousands of options into a select dropdown.
where: [['options', '!=', false], ['fk_entity', 'IS NULL']],
select: ['options']
}, {name: 'options'}];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@
prefix = typeof prefix === 'undefined' ? '' : prefix;
_.each(fields, function(field) {
var item = {
id: prefix + field.name + (field.suffixes && _.includes(field.suffixes, suffix.replace(':', '')) ? suffix : ''),
// Use suffix only if the field is not an FK. EntityRef fields look-up by id.
id: prefix + field.name + (!field.fk_entity && _.includes(field.suffixes || [], suffix.replace(':', '')) ? suffix : ''),
text: field.label,
description: field.description
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,16 @@
return '~/crmSearchTasks/crmSearchInput/boolean.html';
}

if (field.options) {
return '~/crmSearchTasks/crmSearchInput/select.html';
}

if ((field.fk_entity || field.name === 'id') && !_.includes(['>', '<', '>=', '<='], ctrl.op)) {
return '~/crmSearchTasks/crmSearchInput/entityRef.html';
if (!_.includes(['>', '<', '>=', '<='], ctrl.op)) {
// For fields with both an FK and an option list, prefer the FK
// because it's more efficient to render an autocomplete than to
// pre-load potentially thousands of options into a select dropdown.
if ((field.fk_entity || field.name === 'id')) {
return '~/crmSearchTasks/crmSearchInput/entityRef.html';
}
if (field.options) {
return '~/crmSearchTasks/crmSearchInput/select.html';
}
}

if (field.data_type === 'Integer') {
Expand Down

0 comments on commit e2f6270

Please sign in to comment.