EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Allow to display switch for logical field on detail page

Open ksn135 opened this issue 1 year ago • 1 comments

Short description of what this feature will allow to do: I need to enable/disable a logical field directly on the detail page for a specific user group (role-based).

https://github.com/user-attachments/assets/d3c1eaf3-6985-4bef-b3e4-f62b0d6b897b

After looking at the code I was surprised to find out that this is not possible, moreover it is explicitly forbidden and allowed only for the index page. I don't understand why such a restriction was introduced? I suggest to allow the switch to be shown on the detail page as well.

The edits are not big (3 files with one line each), but after yarn build 78 changed files with 129 additions and 126 deletions come up. Screenshot 2024-07-29 at 01 45 35 Screenshot 2024-07-29 at 01 45 04

Example of how to use this feature To preserve the old logic should be used like this (it is BC, may be better add a new method like renderAsSwitchOnDetail()?):

        yield Fields\BooleanField::new('notifyEnabled', 'admin_label.doc.field.notifyEnabled')
            ->renderAsSwitch($pageName === Crud::PAGE_INDEX);

If you want to add a switch only on detail page:

        yield Fields\BooleanField::new('notifyEnabled', 'admin_label.doc.field.notifyEnabled')
            ->renderAsSwitch($pageName === Crud::PAGE_DETAIL);

If you want to display a switch on both pages:

        yield Fields\BooleanField::new('notifyEnabled', 'admin_label.doc.field.notifyEnabled')
            ->renderAsSwitch();

If you want to add more complex logic (allow only on detail page for specific role), use this:

        yield Fields\BooleanField::new('notifyEnabled', 'admin_label.doc.field.notifyEnabled')
            ->renderAsSwitch($pageName === Crud::PAGE_INDEX ? false : $this->isGranted('ROLE_SWITCH_CLICKERS'))
             ->setPermission(DocAccessVoter::SHOW_NOTIFY_ENABLED);

What do you think? I can make a PR.

ksn135 avatar Jul 28 '24 22:07 ksn135

Please make a PR, had the same problem... Thanks for the solution!

p2kkx avatar Oct 11 '24 16:10 p2kkx