Yes, I know this has been asked before and discouraged, but I have a good use case for that. I am interested in learning the view-oriented supplementary approach.
The use case:
I have an entity, say Venue (id, name, capacity)
which I use as collection in EasyAdmin. To render choices, I require this entity to have string representation.
I want the display to say %name% (%capacity% places)
.
As you've correctly guessed, I require the word "places" translated.
I could want to do it
- directly in the entity's
__toString()
method - in form view by properly rendering
__toString()
output
I have no idea how to implement either but I agree that the first approach violates the MVC pattern.
Please advise.
Displaying it as
%name% (%capacity% places)
is just a "possible" representation in your form view so I would shift this very specific representation to your Form Type.What can belong in the
__toString()
method of your Venue entity:messages.en.yml:
Next your Form Type using choice_label (also worth knowing: choice_translation_domain) :
& also register your form type as a service in services.yml: