diff --git a/doc/crud.rst b/doc/crud.rst index eed1d2c542..1c80561c51 100644 --- a/doc/crud.rst +++ b/doc/crud.rst @@ -494,6 +494,13 @@ Templates and Form Options ['validation_groups' => ['my_validation_group']], ['validation_groups' => ['Default'], '...' => '...'], ); + + // this method allows to set a custom css class for each rendered row on `index` page + // argument can be string or callable with current entity as its argument + // return type of the callable must be string + ->setIndexRowClass( + fn ($entityInstance) => $entityInstance->getStatus() + ); ; } diff --git a/src/Config/Crud.php b/src/Config/Crud.php index 9b46740b62..a96de56a37 100644 --- a/src/Config/Crud.php +++ b/src/Config/Crud.php @@ -429,4 +429,11 @@ private function getValidPageNames(): array { return [self::PAGE_DETAIL, self::PAGE_EDIT, self::PAGE_INDEX, self::PAGE_NEW]; } + + public function setIndexRowClass(string|callable $indexRowClass): self + { + $this->dto->setIndexRowClass($indexRowClass); + + return $this; + } } diff --git a/src/Dto/CrudDto.php b/src/Dto/CrudDto.php index a9412e03b0..cc3e674940 100644 --- a/src/Dto/CrudDto.php +++ b/src/Dto/CrudDto.php @@ -25,6 +25,8 @@ final class CrudDto private ?string $entityFqcn = null; private $entityLabelInSingular; private $entityLabelInPlural; + private $indexRowClass; + private array $defaultPageTitles = [ Crud::PAGE_DETAIL => 'page_title.detail', Crud::PAGE_EDIT => 'page_title.edit', @@ -507,4 +509,18 @@ public function hideNullValues(bool $hide): void { $this->hideNullValues = $hide; } + + public function setIndexRowClass(string|callable $indexRowClass): void + { + $this->indexRowClass = $indexRowClass; + } + + public function getIndexRowClass($entityInstance) + { + return match (true) { + \is_string($this->indexRowClass) => $this->indexRowClass, + \is_callable($this->indexRowClass) => \call_user_func($this->indexRowClass, $entityInstance), + default => null, + }; + } } diff --git a/templates/crud/index.html.twig b/templates/crud/index.html.twig index dd6851e6b7..ca7fa0caab 100644 --- a/templates/crud/index.html.twig +++ b/templates/crud/index.html.twig @@ -139,7 +139,7 @@ {% block table_body %} {% for entity in entities %} {% if entity.isAccessible %} -