mvc: simplify getNodeContent() and UIModelGrid usage (https://github.com/opnsense/core/pull/8931) and restore search by description when available.

We can push the conditional description ($) to the model for clarity, in which case the frontend can use the translated value on demand and we don't have to ship values when equal.
This commit is contained in:
Ad Schellevis 2025-07-12 16:45:04 +02:00
parent a87caddc17
commit a0bc7adda9
3 changed files with 11 additions and 22 deletions

View file

@ -123,20 +123,7 @@ class UIModelGrid
// parse rows, because we may need to convert some (list) items we need to know the actual content
// before searching.
$row = [];
$row['uuid'] = $record->getAttributes()['uuid'];
$content = $record->getNodeContent();
foreach ($fields as $fieldname) {
if (array_key_exists($fieldname, $content)) {
$row[$fieldname] = $content[$fieldname];
$descr = '$' . $fieldname;
if (array_key_exists($descr, $content) && $content[$descr] !== $content[$fieldname]) {
// descriptive/translated value available
$row[$descr] = $content[$descr];
}
}
}
$row = array_merge(['uuid' => $record->getAttributes()['uuid']], $record->getNodeContent());
// if a search phrase is provided, use it to search in all requested fields
$search_clauses = preg_split('/\s+/', $searchPhrase);
@ -144,10 +131,8 @@ class UIModelGrid
foreach ($search_clauses as $clause) {
$searchFound = false;
foreach ($fields as $fieldname) {
if (
isset($row[$fieldname]) &&
strpos(strtolower($row[$fieldname]), strtolower($clause)) !== false
) {
$item = $row['$' . $fieldname] ?? $row[$fieldname] ?? ''; /* prefer search by description */
if (!empty($item) && strpos(strtolower($item), strtolower($clause)) !== false) {
$searchFound = true;
}
}

View file

@ -720,8 +720,9 @@ abstract class BaseField
}
/**
* get nodes as array structure using getValue() and getDescription() as leaves, the latter prefixed with a
* dollar sign ($) as these are impossible to exist in our xml structure. (eg field, $field)
* get nodes as array structure using getValue() and (optionally) getDescription() as leaves,
* the latter prefixed with a dollar sign ($) as these are impossible to exist in our xml structure.
* (eg field, $field)
* @return array
*/
public function getNodeContent()
@ -732,7 +733,10 @@ abstract class BaseField
$result[$key] = $node->getNodeContent();
} else {
$result[$key] = $node->getValue();
$result['$' . $key] = $node->getDescription();
$descr = $node->getDescription();
if ($descr != $result[$key]) {
$result['$' . $key] = $descr;
}
}
}
return $result;

View file

@ -110,7 +110,7 @@ class ModelRelationField extends BaseListField
foreach ($searchItems as $uuid => $node) {
$descriptions = [];
foreach ($displayKeys as $displayKey) {
$descriptions[] = $node['$' . $displayKey] ?? '';
$descriptions[] = $node['$' . $displayKey] ?? $node[$displayKey] ?? '';
}
if (isset($modelData['filters'])) {
foreach ($modelData['filters'] as $filterKey => $filterValue) {