EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

avatar_uri_path for EasyAdmin user configuration

Open rernesto opened this issue 7 years ago • 2 comments

I am using Vich uploader for user avatar. Sometimes we only save on database the filename and the path we do it as a parameter or something similar. I have this parameter on my services,yaml:

parameters:
    app.path.user_profile: /upload/images/user_profile
...

EasyAdmin user configuration could be:

easy_admin:
    user:
        avatar_property_path: 'image'
        avatar_uri_path: '%app.path.user_profile%/'
...

or (keys make more sense this way)

easy_admin:
    user:
        avatar_property_filename: 'image'
        avatar_uri_path: '%app.path.user_profile%/'

How I am overriding layout.html.twig I did it using a twig global variable

twig:
    globals:
        avatar_uri_path: '%app.path.user_profile%/'
...
...
<img class="user-avatar" src="{{ avatar_uri_path ~ _avatar_image_path }}" />
...

screenshot from 2019-02-12 10-54-20

rernesto avatar Feb 12 '19 15:02 rernesto

@rernesto when we introduced this feature, I knew some edge-cases would be unsolved. However, given that the value of the current config option is a property path and given that it can refer to a PHP method (which can literally do anything) I prefer to not complicate this anymore.

In your case, I propose to add a method getAvatarUri() or something like that to the User entity and use avatar_uri as the property path. Inside that method, you can hardcode the '/upload/images/user_profile' string or better, define it as a PHP constant inside the class ... and refer to that constant inside the Symfony configuration file.

I'm sorry if this solution doesn't satisfy you ... but I hope you understand that we don't want to complicate things too much. Thanks.

javiereguiluz avatar Feb 12 '19 19:02 javiereguiluz

@rernesto I did this but it dosen't Work! (I use sumfony 4, Easyadmin 2.0)

entities:
        User:
            class: App\Entity\User
            avatar_property_path: 'image'
            avatar_uri_path: '%app.path.user_images%/'
twig:
    . . .
    globals:
        avatar_uri_path: '%app.path.user_images%/'
<img class="user-avatar" src="{{ avatar_uri_path ~ _avatar_image_path }}" />
parameters:
    . . . 
    app.path.user_images: /uploads/images/users

Thanks in advance

WassimGit18 avatar Apr 16 '19 15:04 WassimGit18