mvc: refactor grid search to fetch descriptive values from the model instead of trying to reconstruct them.

This makes it easier for special model field types to translate values into human readable formats, also when not being presented to the user like that in a selector.
This commit is contained in:
Ad Schellevis 2024-04-04 18:19:12 +02:00
parent 4b647fc3f3
commit 10c81a4eea
3 changed files with 26 additions and 16 deletions

View File

@ -127,22 +127,7 @@ class UIModelGrid
$row['uuid'] = $record->getAttributes()['uuid'];
foreach ($fields as $fieldname) {
if ($record->$fieldname != null) {
$row[$fieldname] = $record->$fieldname->getNodeData();
if (is_array($row[$fieldname])) {
$listItems = $row[$fieldname];
$row[$fieldname] = '';
foreach ($listItems as $fieldValue) {
if ($fieldValue['selected'] == 1) {
if ($row[$fieldname] != "") {
$row[$fieldname] .= ',';
}
$row[$fieldname] .= $fieldValue['value'];
}
}
if ($row[$fieldname] === null) {
$row[$fieldname] = "";
}
}
$row[$fieldname] = $record->$fieldname->getDescription();
}
}

View File

@ -651,6 +651,17 @@ abstract class BaseField
return (string)$this;
}
/**
* Return descriptive value of the item.
* For simple types this is usually the internal value, complex types may return what this value represents.
* (descriptions of selected items)
* @return null|string
*/
public function getDescription()
{
return (string)$this;
}
/**
* update model with data returning missing repeating tag types.
* @param $data array structure containing new model content

View File

@ -122,6 +122,20 @@ abstract class BaseListField extends BaseField
return $result;
}
/**
* {@inheritdoc}
*/
public function getDescription()
{
$items = [];
foreach ($this->getNodeData() as $fieldValue) {
if ($fieldValue['selected'] == 1) {
$items[] = $fieldValue['value'];
}
}
return implode(',', $items);
}
/**
* {@inheritdoc}