Skip to content

Commit

Permalink
feat: add second and third argument to LinkColumnType callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Sep 6, 2024
1 parent 06933bf commit d9478d7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 14 additions & 3 deletions docs/src/reference/types/column/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@ The [`LinkColumnType`](https://github.com/Kreyu/data-table-bundle/blob/main/src/

Sets the value that will be used as a [href attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-href).

The callback will always receive three arguments:

- first represents a data of the column
- second represents a data of the row
- third is always an instance of the column

Let's assume, that we're displaying a list of products, and each product has one category:

```php
use App\Entity\Category;
use App\Entity\Product;
use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\LinkColumnType;

$builder
->addColumn('category', LinkColumnType::class, [
'href' => function (Category $category): string {
return $this->urlGenerator->generate('category_show', [
'id' => $category->getId(),
'href' => function (Category $category, Product $product, ColumnInterface $column): string {
return $this->urlGenerator->generate('product_category_show', [
'product' => $product->getId(),
'category' => $category->getId(),
]);
},
])
Expand Down
4 changes: 2 additions & 2 deletions src/Column/Type/LinkColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ final class LinkColumnType extends AbstractColumnType
public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
{
if (is_callable($href = $options['href'])) {
$href = $href($view->vars['data']);
$href = $href($view->vars['data'], $view->parent->data, $column);
}

if (is_callable($target = $options['target'])) {
$target = $target($view->vars['data']);
$target = $target($view->vars['data'], $view->parent->data, $column);
}

$view->vars = array_replace($view->vars, [
Expand Down

0 comments on commit d9478d7

Please sign in to comment.