Skip to content

Add display_if support to mappers (Form, List, Show, Datagrid) #8311

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

Draft
wants to merge 2 commits into
base: 4.x
Choose a base branch
from

Conversation

marshallhampton56
Copy link

Subject

Introduce display_if option to FormMapper, ShowMapper, ListMapper, and DatagridMapper to conditionally add fields based on runtime logic.

This allows dynamic control over which fields appear in the form, show view, list, or filter

I am targeting this branch, because this is a new backwards-compatible feature.

Changelog

### Added
- `display_if` option support for `FormMapper`, `ShowMapper`, `ListMapper`, and `DatagridMapper` to conditionally skip adding fields based on a callable
- Tests for `display_if` in `FormMapperTest`, `ShowMapperTest`, `ListMapperTest`, and `DatagridMapperTest`
- Documentation for `display_if` usage 

@VincentLanglet
Copy link
Member

Why not doing

if ($foo) {
     $formMapper->add('bar');
}

or

$formMapper->ifTrue($foo)->add('bar')->ifEnd();

@zyberspace
Copy link
Contributor

Hi,
i really like this feature. I have a lot of conditions in my admins with ->ifTrue($this->getSubject()->getSomething()) or plain simple ifs if (...) {...}. I feel like this new option could help improve readability in a lot of smaller cases, e.g. when the if is only for one single field.

Is there a reason you sometimes pass the admin and sometimes the object? I would prefer the first argument to always be the current admin and then the object as second argument, if available.

@VincentLanglet
Copy link
Member

Hi, i really like this feature.

Honestly, I don't.

Keep in mind SonataAdmin is almost non maintained anymore. I'm the only one reviewing/releasing and I don't

  • use this lib anymore for 2 years
  • plan to spend a lot of time maintaining it for free

Regarding this, adding features which are not needed because they can be done another way, gives no benefit, just more code to maintain.

I have a lot of conditions in my admins with ->ifTrue($this->getSubject()->getSomething()) or plain simple ifs if (...) {...}. I feel like this new option could help improve readability in a lot of smaller cases, e.g. when the if is only for one single field.

->ifTrue($this->getSubject()->getSomething())->add('foo')->ifEnd()

is at least as readable as

->add('foo', [
     'display_if' => static fn($subject) => $this->getSubject()->getSomething())
])

AND avoid the following issue:

Is there a reason you sometimes pass the admin and sometimes the object? I would prefer the first argument to always be the current admin and then the object as second argument, if available.

@haivala
Copy link

haivala commented Jul 8, 2025

Keep in mind SonataAdmin is almost non maintained anymore. I'm the only one reviewing/releasing and I don't

* use this lib anymore for 2 years

* plan to spend a lot of time maintaining it for free

Off topic but I'm curious: What do you use then? Any suggestions to where to migrate?

@VincentLanglet
Copy link
Member

Off topic but I'm curious: What do you use then? Any suggestions to where to migrate?

  • My new job doesn't require an admin bundle, so I don't use anymore.
  • For personal use I recently used EasyAdminBundle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants