EasyCorp / EasyCorp/EasyAdminBundle

Feature request: a displayIf(callable) for Fields.

Open
#7,543 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
4.3k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
11

Description

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;
    }

    ..............

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the proposed integration points in src/Field/FieldTrait.php, src/Dto/FieldDto.php, and src/Factory/FieldFactory.php, especially processFields(). Verify the callback receives the entity instance and that fields are omitted when it returns false on detail, edit, and new pages while remaining available on index.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, symfony
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.