EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.02k stars 1.02k forks source link

Add addDisplayOnPage in src/Field/FieldTrait.php #6318

Open ceponcet opened 1 month ago

ceponcet commented 1 month ago

Short description of what this feature will allow to do: Add addDisplayOnPage(string $pageName) in src/Field/FieldTrait.php

Adding the addDisplayOnPage(string $pageName) method to the src/Field/FieldTrait.php trait allows adding a field to a "page" other than EDIT INDEX DETAIL NEW. These other pages can be, for example, CSV or WHATEVER.

Example of how to use this feature

#src/Controller/Admin/MyCrudController.php
[...]
public function configureFields(string $pageName): iterable
{
      yield TextField::new('field')
            ->addDisplayOnPage("CSV")
            ->hideOnIndex()->hideOnForm()->hideOnDetail();
}

# And in another method, We retrieve only fields of CSV PAGE
public function export(AdminContext $context): Response
{
     //
        $context->getCrud()->setPageName("CSV");
     //
}
Seb33300 commented 1 month ago

In order to be consistent with existing method names, I suggest to rename addDisplayOnPage(string $pageName) into onlyOn(string $pageName) + another hideOn(string $pageName)

A workaround for now should be:

public function configureFields(string $pageName): iterable
{
    if ('CSV' == $pageName) {
        yield TextField::new('field');
    }
}