Search code examples
liferayliferay-7liferay-7.1

How to fetch the author profile picture on WebContent template?


Can someone tell me how to fetch the web content author image in web content template? When I tried with the user object, it's returning the current logged in user and not the actual one.

I can fetch the author name and id by using the key

<#assign authorId = .vars['reserved-article-author-id'].data>

<#assign authorName = .vars['reserved-article-author-name'].data>

I have used the below code to fetch the content author image but it returns the current loggedin user.

<#assign author-img = user.getPortraitURL(themeDisplay)>

But it is returning the current logged in user image

I have also tried with the taglib

<@liferay_ui["user-display"] userId="userId" />, but its failing with a message "Failed to set JSP tag parameter "author" (declared type: boolean, actual value's type: String). See cause exception for the more specific cause...

Thanks


Solution

  • I can't tell you about the solution from the top of my head, but here's what happens:

    <#assign author-img = user.getPortraitURL(themeDisplay)>
    

    uses the predefined variable user, e.g. the currently logged in user. Exactly what you get, but not what you want. themeDisplay is used to provide the current web context (e.g. host name that's being used) in, for proper URL generation that matches the currently displayed page.

    Instead of using reserved-article-author-name, you want to use reserved-article-author-id to get hold of the user object with this id. You'll need UserLocalService for that (pardon my memory - I'm rarely using freemarker and would probably mess up how to actually do that, but you can search for that information. You might need access to the serviceLocator - which makes a good additional search term).

    Having that user object (name it differently than user, e.g. author), you can get the author's portrait image URL with the same strategy as above:

    <#assign author-img = author.getPortraitURL(themeDisplay)>