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

feat: add Field::displayIf method #6451

Open
wants to merge 3 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
11 changes: 11 additions & 0 deletions src/Dto/FieldDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class FieldDto
/** @internal */
private $uniqueId;
private KeyValueStore $displayedOn;
private $displayCallable;
private array $htmlAttributes = [];

public function __construct()
Expand Down Expand Up @@ -478,6 +479,16 @@ public function isDisplayedOn(string $pageName): bool
return $this->displayedOn->has($pageName);
}

public function isDisplayed(?EntityDto $entityDto = null): bool
{
return null === $this->displayCallable || (bool) \call_user_func($this->displayCallable, $entityDto?->getInstance());
}

public function setDisplayCallable(callable $displayCallable): void
{
$this->displayCallable = $displayCallable;
}

public function getHtmlAttributes(): array
{
return $this->htmlAttributes;
Expand Down
4 changes: 3 additions & 1 deletion src/Factory/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo
$isDetailOrIndex = \in_array($currentPage, [Crud::PAGE_INDEX, Crud::PAGE_DETAIL], true);
foreach ($fields as $fieldDto) {
if ((null !== $currentPage && false === $fieldDto->isDisplayedOn($currentPage))
|| false === $this->authorizationChecker->isGranted(Permission::EA_VIEW_FIELD, $fieldDto)) {
|| false === $fieldDto->isDisplayed($entityDto)
|| false === $this->authorizationChecker->isGranted(Permission::EA_VIEW_FIELD, $fieldDto)
) {
$fields->unset($fieldDto);

continue;
Expand Down
7 changes: 7 additions & 0 deletions src/Field/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ public function onlyWhenUpdating(): self
return $this;
}

public function displayIf(callable $callable): self
{
$this->dto->setDisplayCallable($callable);

return $this;
}

/**
* @param int|string $cols An integer with the number of columns that this field takes (e.g. 6),
* or a string with responsive col CSS classes (e.g. 'col-6 col-sm-4 col-lg-3')
Expand Down
Loading