Add the Author Profile Picture on WebContent - liferay

I have a Liferay WebContent that is based on a Structure and Template. I want to display the Author picture (from Liferay UserProfile) as part of the Template.
Is there a way to access that Information with Velocity?
I found this variable:
$reserved-article-author-id
However, is there an easy way to get the Picture using the AuthorID?
In the profile page, pictures are displayed this way:
The imgid is not the same as the userid. Is there an easy way to get the imgid from the userid?
Liferay Version is 6.1.

This should work on 6.1:
#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($user = $userLocalService.fetchUserById($getterUtil.getLong($reserved-article-author-id.data)))
#set ($profilePicUrl = $request.theme-display.path-image + "/user_")
#if ($user.isFemale())
#set ($profilePicUrl = $profilePicUrl + "female")
#else
#set ($profilePicUrl = $profilePicUrl + "male")
#end
#set ($profilePicUrl = $profilePicUrl + "_portrait?img_id=")
#set ($profilePicUrl = $profilePicUrl + $user.getPortraitId())
<img src="$profilePicUrl" />
Note that you must have journal.template.velocity.restricted.variables= in your portal-ext.properties file, to allow access to $serviceLocator

thanks for the answer, helped me out!
just a little thing
#set ($profilePicUrl = $request.theme-display.path-image + "/user_")
didnt work for me in liferay 6.2 GA2, it seems $request.theme-display.path-image doesnt work in the new version
but i solved like this:
#set ($profilePicUrl = "$theme_display.getPathImage()" + "/user_")
hope it helps someone :)

This work on 6.2:
Try this:
#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($jaId = $getterUtil.getString($reserved-article-id.data))
#set ($ja = $journalArticleLocalService.getArticle($getterUtil.getLong($groupId),$jaId))
#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($usuario = $userLocalService.getUserById($getterUtil.getLong($ja.getUserId())))
#set ($tD = $request.get("theme-display")) #set ($DigesterUtil = $portal.getClass().forName("com.liferay.portal.kernel.util.DigesterUtil"))
#set ($DigesterUtil = $portal.getClass().forName("com.liferay.portal.kernel.util.DigesterUtil"))
#set ($profilePicUrl = $profilePicUrl + "_portrait?img_id=")
#set ($profilePicUrl = $profilePicUrl + $usuario.getPortraitId())
#set ($profilePicUrl = $tD.get("path-image") + "/user_")
#if ($usuario.isFemale())
#set ($profilePicUrl = $profilePicUrl + "female")
#else
#set ($profilePicUrl = $profilePicUrl + "male")
#end
#set ($profilePicUrl = $profilePicUrl + "_portrait?img_id=")
#set ($profilePicUrl = $profilePicUrl + $usuario.getPortraitId())
#set ($profilePicUrl = $profilePicUrl + "&img_id_token=")
#set ($profilePicUrl = $profilePicUrl + $httpUtil.encodeURL($DigesterUtil.digest($usuario.getUuid())))
<div class="img_user">
<img src="$profilePicUrl"/>
</div>

Here is smaller version for Liferay 6.2:
#set($userConstants = $portal.getClass().forName("com.liferay.portal.model.UserConstants"))
#set($portraitUrl = $userConstants.getPortraitURL($request.theme-display.path-image, $user.male, $user.portraitId, $user.userUuid))
Regards,
Martin

Related

Velocity adding values with in concatenation statement

Apologies if this has been asked before but i could not find a straightforward answer.
I have the following code, the application is a third party so the incoming $supernet is provided as a string. So what i am wanting to do it take a IP address and then manipulate it to create a list of subnets based on a template.
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split("\."))
#set ($netmgmt[2] = "$octs[0].$octs[1].$octs[2].$octs[2]+1")
$netmgmt
But this as I expect gives the result "10.10.10.10+1"
I figured out I can do this in a longhand way
convert the $oct[2] to an integer and store in a variable
create a temporary variable and store the result of the converted $oct[2] + 1
Then use this to create the string
like
$Integer = $convert.toInteger($oct[2])
#set ($newoct = $Integer + 1)
#set ($netmgmt[2] = "$octs[0].$octs[1].$octs[2].$newoct")
but i have a lot of these to create and this will get long winded and confusing.
So i am trying to work out if I can write #set ($octs = $Supernet.split(".")) to store as integers. To be honest just trying to directly convert each array value to int does not seem to work as based on what i have found on line. I expected the below would result in "$oct1 = 10" but it does not?
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split("\."))
#set ($oct1 = $convert.toInteger($oct[0])
New to Velocity any help appreciated.
You should check which version of Velocity-Tools you are using, and which tools are available in the provided context.
Since Velocity-Tools 3.1, for instance, $convert.toInteger() has been deprecated in favor of $math.toInteger(). You can check if those tools are present in the context just by displaying $convert and $math.
The following code works for me with Velocity 1.7 + Tools 2.0, as long as with Velocity 2.X + Tools 3.x:
#set ($Supernet = "10.10.10.10")
#set ($octs = $Supernet.split('\.'))
#set ($last = $math.toInteger($octs[2]) - 1)
#set ($ip = "${octs[0]}.${octs[1]}.${octs[2]}.$last")
$ip

how to set asset publisher tag with hard coded

I added an asset publisher to liferay and add it to "portal-normal.vm" with "Portlet Id".
But this portlet (added with portlet Id to my theme) Does not inherit that portlet settings.
Now I want to set asset publisher tag with hard code, but I don't know how do it.
#set ($VOID = $velocityPortletPreferences.setValue('display-style', '1'))
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
#set ($instanceId = '6nro')
#set ($myPortletId = "101_INSTANCE_${instanceId}")
#set ($portletSetup = $portletPreferencesFactoryUtil.getLayoutPortletSetup($layout, $myPortletId))
$theme.runtime($myPortletId, $queryString, $velocityPortletPreferences.toString())
$velocityPortletPreferences.reset()

Liferay, how to initiate the certain web content in Velocity

I've been playing around of this issue for awhile now and can't get my head wrapped around of it. I'm using Liferay 6.1 CE GA2.
Goal:
User editable content, for example footer in each page. I've created the web content which id is 12701.
Method:
#set ($local_temp_content = $journalContentUtil.getContent($scope_group_id, "12701", null, "$locale", $theme_display))
$local_temp_content<br />
Issue:
It won't return anything sensible. It's just printing "$local_temp_content" as the result.
Any pointers how to debug this issue?
This is a velocity macro to retrieve a web content by ID from local scope first and then by global scope:
#macro(glarticle $temp_article_id)
#set ($temp_content = "")
#set ($scope_group_id = $theme_display.scopeGroupId)
#set ($global_group_id = $theme_display.companyGroupId)
#set ($temp_content = $journalContentUtil.getContent($scope_group_id, $temp_article_id, null, "$locale", $theme_display))
#set ($temp_content = "$!{temp_content}")
#if ($temp_content.length() == 0)
#set ($temp_content = $journalContentUtil.getContent($global_group_id, $temp_article_id, null, "$locale", $theme_display))
#end
$!{temp_content}
#end
How to use it:
#glarticle('1234')
For debugging velocity try outputting every part of your call.
scope_group_id = $scope_group_id<br>
theme_display = $theme_display<br>
journalContentUtil = $journalContentUtil<br>
If you get exactly what you wrote than that variable is not available.
If all are resolved then possibilitis are:
wrong article id
there was exception during article rendering (you should check log)

Liferay - Error setting preferences of sitemap portlet in Theme

I am adding a sitemap portlet at the footer of my theme, and when I instance it I set the preferences for the root layout id, display depth and show hidden pages.
My portal-normal.vm snippet:
#* Sitemap for the Footer Links *#
#set ($portlet_id = '85')
#set ($instance_id = 'AAAC')
#* Preferences *#
#set ($rootLayoutId = "f74bd692-715f-4532-8490-dee211bebed8")
#set ($displayDepth = 0)
#set ($showHiddenPages = true)
#set ($myPortletId = "${portlet_id}_INSTANCE_${instance_id}")
$velocityPortletPreferences.setValue('portlet-setup-show-borders', 'false')
$velocityPortletPreferences.setValue('rootLayoutId', 'f74bd692-715f-4532-8490-dee211bebed8')
$velocityPortletPreferences.setValue('displayDepth', '0')
$velocityPortletPreferences.setValue('showHiddenPages', 'false')
$theme.runtime($myPortletId, '', $velocityPortletPreferences.toString())
#set ($VOID = $velocityPortletPreferences.reset())
But the portlet is displayed with the basic preferences.
Haven't found info about preferences names/values, so any help would be appreciated.
Thanks.
EDIT with the solution
Following #Pankaj Kathiriya indication i wrote next code that is correctly working:
#* Sitemap for the Footer Links *#
#set ($portlet_id = '85')
#set ($instance_id = 'AABB')
#* Instanciate the portlet *#
#set ($myPortletId = "${portlet_id}_INSTANCE_${instance_id}")
$velocityPortletPreferences.setValue('portlet-setup-show-borders', 'false')
$velocityPortletPreferences.setValue('rootLayoutUuid', 'f74bd692-715f-4532-8490-dee211bebed8')
$velocityPortletPreferences.setValue('displayDepth', '0')
$velocityPortletPreferences.setValue('showHiddenPages', 'true')
$theme.runtime($myPortletId, '', $velocityPortletPreferences.toString())
#* Reset preferences *#
$velocityPortletPreferences.reset()
Problem is at line below:
$velocityPortletPreferences.setValue('rootLayoutId', 'f74bd692-715f-4532-8490-dee211bebed8')
It should be
$velocityPortletPreferences.setValue('rootLayoutUuid', 'f74bd692-715f-4532-8490-dee211bebed8')
It should be rootLayoutUuid
Regards

How to get image using structure and template to get title,small and large image

I am using liferay 6.1.20.
Structure variable demo_image : type Document and Media
here is my template code. It is not fetching uuid or groupId from url !
#set ($dlLocalService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLAppLocalService"))
#set ($url = $getterUtil.getString($demo_image.getData()))
#set ($uuid = $getterUtil.getString($httpUtil.getParameter($url, "uuid", false)))
#set ($groupId = $getterUtil.getLong($httpUtil.getParameter($url, "groupId", false)))
#set ($imageObj = $dlLocalService.getFileEntryByUuidAndGroupId($uuid,$groupId))
#set ($imageSmallid = $imageObj.getSmallImageId())
#set ($imageLargeid = $imageObj.getLargeImageId())
#set ($imageTitle = $imageObj.getTitle())
#set ($imageDescription = $imageObj.getDescription())
#set ($urlLargeImage = "/documents/imageLargeid")
#set ($urlSmallImage = "/documents/imageSmallid")
$imageTitle
<img src="$urlSmallImage" rel="$imageTitle" alt="$imageTitle" />
Check value of $url for your image, does this URL contain uuid & groupId if yes you should get the values. else if url contains imageId try using different method from service util.
Also try using DLFileE‌​ntryLocalService instead of DLAppLocalService
Hope this helps you find your solution !

Resources