In an EasyAdmin 3 / Symfony 5.2 backend, I have several *CrudController classes with datetime fields which are all configured like:
public function configureFields(string $pageName): iterable
{
return [
// ...
DateTimeField::new('createdAt')
->setTimezone($this->getUser()->getTimezone())
->setFormTypeOption('view_timezone', $this->getUser()->getTimezone()),
];
}
Instead of copy paste the same code for every datetime field of every entity, is there a way to define the timezone once as default ?
Edit:
I found out that setTimezone()
can be called once for the whole *CrudController class in ConfigureCrud()
and this applies by default to all fields:
class MyCrudController extends AbstractCrudController
{
public function configureCrud(Crud $crud): Crud
{
return $crud->setTimezone($this->getUser()->getTimezone());
}
}
Actually it can even be set on the dashboard to apply as default for all its crud controllers and their fields.
class DashboardController extends AbstractDashboardController
{
public function configureCrud(): Crud
{
// Default config for all cruds in this controller.
// Only impact index and detail actions.
// For Forms, use ->setFormTypeOption('view_timezone', '...')
// on all fields
return Crud::new()->setTimezone($this->getUser()->getTimezone());
}
}
So, the configureCrud()
in the DashboardController class is the easiest solution to for index and detail actions.
I'm still interested in a similar solution to avoir setFormTypeOption('view_timezone', '...')
on every field.