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

Method setIndexRowClass() for Config\Crud #6448

Open
wants to merge 4 commits into
base: 4.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions doc/crud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Config/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions src/Dto/CrudDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
};
}
}
2 changes: 1 addition & 1 deletion templates/crud/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
{% block table_body %}
{% for entity in entities %}
{% if entity.isAccessible %}
<tr data-id="{{ entity.primaryKeyValueAsString }}">
<tr data-id="{{ entity.primaryKeyValueAsString }}" class="{{ ea.crud.indexRowClass(entity.instance) }}">
{% if has_batch_actions %}
<td class="batch-actions-selector">
<div class="form-check">
Expand Down
Loading