@@ -221,13 +221,11 @@ protected function fetchColumnSuggestions($searchTerm)
221
221
$ model = $ this ->getModel ();
222
222
$ query = $ model ::on ($ this ->getDb ());
223
223
224
- // Collect custom variables first and check for exact matches
224
+ // Collect custom variables
225
225
$ parsedArrayVars = [];
226
226
$ varSuggestions = [];
227
- $ exactVarMatches = [];
228
- $ halfLimit = (int ) (static ::DEFAULT_LIMIT / 2 );
229
-
230
227
$ exactSearchTerm = trim ($ searchTerm , ' * ' );
228
+ $ titleAdded = false ;
231
229
if ($ exactSearchTerm !== '' ) {
232
230
foreach ($ this ->getDb ()->select ($ this ->queryCustomvarConfig ($ searchTerm )) as $ customVar ) {
233
231
$ search = $ name = $ customVar ->flatname ;
@@ -245,68 +243,60 @@ protected function fetchColumnSuggestions($searchTerm)
245
243
if (isset ($ customVar ->$ relation )) {
246
244
$ varRelation = $ relation . '.vars. ' . $ search ;
247
245
$ varLabel = sprintf ($ label , $ name );
246
+ // Suggest exact custom variable matches first
248
247
if ($ name === $ exactSearchTerm ) {
249
- $ exactVarMatches [$ varRelation ] = $ varLabel ;
250
- } elseif ($ this ->matchSuggestion ($ varRelation , $ varLabel , $ searchTerm )) {
248
+ if ($ titleAdded === false ) {
249
+ $ this ->addHtml (HtmlElement::create (
250
+ 'li ' ,
251
+ ['class ' => static ::SUGGESTION_TITLE_CLASS ],
252
+ t ('Best Suggestions ' )
253
+ ));
254
+
255
+ $ titleAdded = true ;
256
+ }
257
+
258
+ yield $ varRelation => $ varLabel ;
259
+ } else {
251
260
$ varSuggestions [$ varRelation ] = $ varLabel ;
252
261
}
253
262
}
254
263
}
255
264
}
256
265
}
257
266
258
- // Adjust the number of columns to be suggested based on custom variable count
259
- $ varCount = count ($ exactVarMatches ) + count ($ varSuggestions );
260
- $ colLimit = $ halfLimit ;
261
- if ($ varCount < $ halfLimit ) {
262
- $ colLimit = $ halfLimit + ($ halfLimit - $ varCount );
263
- }
264
-
265
- // Exact custom variable matches first
266
- if (! empty ($ exactVarMatches )) {
267
- $ this ->addHtml (HtmlElement::create (
268
- 'li ' ,
269
- ['class ' => static ::SUGGESTION_TITLE_CLASS ],
270
- t ('Best Suggestions ' )
271
- ));
272
-
273
- foreach ($ exactVarMatches as $ relation => $ label ) {
274
- yield $ relation => $ label ;
275
- }
276
- }
277
-
278
267
// Ordinary columns comes after exact matches,
279
268
// or if there ar no exact matches they come first
280
- $ colCount = 0 ;
269
+ $ titleAdded = false ;
281
270
foreach (self ::collectFilterColumns ($ model , $ query ->getResolver ()) as $ columnName => $ columnMeta ) {
282
- if ($ colCount > $ colLimit ) {
283
- break ;
284
- }
285
-
286
271
if ($ this ->matchSuggestion ($ columnName , $ columnMeta , $ searchTerm )) {
287
- if ($ colCount === 0 ) {
272
+ if ($ titleAdded === false ) {
288
273
$ this ->addHtml (HtmlElement::create (
289
274
'li ' ,
290
275
['class ' => static ::SUGGESTION_TITLE_CLASS ],
291
276
t ('Columns ' )
292
277
));
293
- }
294
278
295
- $ colCount ++;
279
+ $ titleAdded = true ;
280
+ }
296
281
297
282
yield $ columnName => $ columnMeta ;
298
283
}
299
284
}
300
285
301
286
// Finally, the other custom variable suggestions
302
- if (! empty ($ varSuggestions )) {
303
- $ this ->addHtml (HtmlElement::create (
304
- 'li ' ,
305
- ['class ' => static ::SUGGESTION_TITLE_CLASS ],
306
- t ('Custom Variables ' )
307
- ));
308
-
309
- foreach ($ varSuggestions as $ relation => $ label ) {
287
+ $ titleAdded = false ;
288
+ foreach ($ varSuggestions as $ relation => $ label ) {
289
+ if ($ this ->matchSuggestion ($ relation , $ label , $ searchTerm )) {
290
+ if ($ titleAdded === false ) {
291
+ $ this ->addHtml (HtmlElement::create (
292
+ 'li ' ,
293
+ ['class ' => static ::SUGGESTION_TITLE_CLASS ],
294
+ t ('Custom Variables ' )
295
+ ));
296
+
297
+ $ titleAdded = true ;
298
+ }
299
+
310
300
yield $ relation => $ label ;
311
301
}
312
302
}
0 commit comments