It would be nice to have a displayIf(callable) method for Fields, similar to the one used for Actions.
It would be useful, in detail, edit, and new, to set a criterion for displaying the field according to the properties of the Entity, e.g.:
yield TextField::new('reason')->displayIf(fn (?Order $entity) => $entity?->isCancelled());
The implementation could be:
// src/Field/FieldTrait.php
public function displayIf(callable $callable): self
{
$this->dto->setDisplayCallable($callable);
return $this;
}
// src/Dto/FieldDto.php
private $displayCallable;
public function setDisplayCallable(callable $displayCallable): void
{
$this->displayCallable = $displayCallable;
}
public function shouldBeDisplayedFor(EntityDto $entityDto): bool
{
return null === $this->displayCallable || \call_user_func($this->displayCallable, $entityDto->getInstance());
}
// src/Factory/FieldFactory.php
public function processFields(EntityDto $entityDto, FieldCollection $fields, string $currentPage): void
..............
if (Crud::PAGE_INDEX !== $currentPage && false === $fieldDto->shouldBeDisplayedFor($entityDto)) {
$fields->unset($fieldDto);
continue;
}
..............
}
It would be nice to have a displayIf(callable) method for Fields, similar to the one used for Actions.
It would be useful, in detail, edit, and new, to set a criterion for displaying the field according to the properties of the Entity, e.g.:
The implementation could be: