Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass row data as second argument of the "formatter" column option #136

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading