EasyAdmin: Display integer cent-amount as Euro/Dollar (like MoneyType with `divisor`) in list view
Solution 1:
MoneyType
is not supported in list view, see https://github.com/EasyCorp/EasyAdminBundle/issues/1995
Here's the workaround outlined at https://github.com/EasyCorp/EasyAdminBundle/issues/1995#issuecomment-356717049 :
-
Create a file
/templates/easy_admin/money.html.twig
with:{% if value is null %} {{ include(entity_config.templates.label_null) }} {# otherwise `null` will be displayed as `0,00 €` #} {% else %} {{ (value/100)|number_format(2, ',', '.') }} € {# you can omit the `number_format()` filter if you don’t want the cents to be shown #} {% endif %}
See
/vendor/easycorp/easyadmin-bundle/src/Resources/views/default/field_integer.html.twig
for the template that EasyAdmin uses by default. -
Activate your new template in
easy_admin.yaml
:list: fields: - {property: 'centAmount', template: 'easy_admin/money.html.twig' }
Result: 900,00 €
Reference: https://symfony.com/doc/current/bundles/EasyAdminBundle/book/list-search-show-configuration.html#rendering-entity-properties-with-custom-templates
Solution 2:
I had the same issue with the MoneyField (Easy Admin 3).
This is how I fixed it:
MoneyField::new('price','Prix')
->setCurrency('EUR')
->setCustomOption('storedAsCents', false);