Skip to content

Commit

Permalink
Pass row data as second argument of the "formatter" column option (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu authored Oct 5, 2024
1 parent 110f35a commit cc07580
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
4 changes: 3 additions & 1 deletion docs/src/reference/types/column/options/column.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ Formats the value to the desired string.
```php
$builder
->addColumn('quantity', NumberColumnType::class, [
'formatter' => fn (float $value) => number_format($value, 2) . 'kg',
'formatter' => function (float $value, Product $product, ColumnInterface $column, array $options) {
return number_format($value, 2) . $product->getUnit();
},
])
;
```
Expand Down
30 changes: 17 additions & 13 deletions src/Column/Type/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function buildValueView(ColumnValueView $view, ColumnInterface $column, a
$rowData = $view->parent->data;

$normData = $this->getNormDataFromRowData($rowData, $column, $options);
$viewData = $this->getViewDataFromNormData($normData, $column, $options);
$viewData = $this->getViewDataFromNormData($normData, $rowData, $column, $options);

$view->data = $normData;
$view->value = $viewData;
Expand Down Expand Up @@ -146,8 +146,10 @@ public function buildExportValueView(ColumnValueView $view, ColumnInterface $col
'formatter' => $options['formatter'],
];

$normData = $this->getNormDataFromRowData($view->parent->data, $column, $options['export']);
$viewData = $this->getViewDataFromNormData($normData, $column, $options['export']);
$rowData = $view->parent->data;

$normData = $this->getNormDataFromRowData($rowData, $column, $options['export']);
$viewData = $this->getViewDataFromNormData($normData, $rowData, $column, $options['export']);

if ($this->translator && is_string($viewData)) {
$translationDomain = $options['export']['value_translation_domain']
Expand Down Expand Up @@ -230,39 +232,41 @@ public function getParent(): ?string
* - using the property accessor with the "property_path" option;
* - falling back to the unmodified column data;
*/
private function getNormDataFromRowData(mixed $data, ColumnInterface $column, array $options): mixed
private function getNormDataFromRowData(mixed $rowData, ColumnInterface $column, array $options): mixed
{
if (null === $data) {
if (null === $rowData) {
return null;
}

if (is_callable($getter = $options['getter'])) {
return $getter($data, $column, $options);
return $getter($rowData, $column, $options);
}

$propertyPath = $options['property_path'] ?? $column->getName();

if ((is_string($propertyPath) || $propertyPath instanceof PropertyPathInterface) && (is_array($data) || is_object($data))) {
return $options['property_accessor']->getValue($data, $propertyPath);
if ((is_string($propertyPath) || $propertyPath instanceof PropertyPathInterface) && (is_array($rowData) || is_object($rowData))) {
return $options['property_accessor']->getValue($rowData, $propertyPath);
}

return $data;
return $rowData;
}

/**
* Retrieves the column view data from the norm data by applying the formatter if given.
*/
private function getViewDataFromNormData(mixed $data, ColumnInterface $column, array $options): mixed
private function getViewDataFromNormData(mixed $normData, mixed $rowData, ColumnInterface $column, array $options): mixed
{
if (null === $data) {
if (null === $normData) {
return null;
}

$viewData = $normData;

if (is_callable($formatter = $options['formatter'])) {
$data = $formatter($data, $column, $options);
$viewData = $formatter($normData, $rowData, $column, $options);
}

return $data;
return $viewData;
}

/**
Expand Down

0 comments on commit cc07580

Please sign in to comment.