Liferay 6.1 get themeDisplay in velocity (vm) file - liferay

I'm trying to get the profile picture of my users and display it in my liferay theme. So far I've been able to access the User object trough the velocity $user variable. The user model has a method called getPortraitUrl() which takes ThemeDisplay as a parameter. I've read in the liferay documentation that the themeDisplay object is available at runtime when working with themes, but I can't seem to make this work.
I've tried several different ways to get the themeDisplay object and none of them seem to work:
$theme
$themeDisplay
$request.get("theme-display")
$theme_display
... and several other methods that return nothing.
$user.getPortraitUrl($themeDisplay) is what I'm trying to achieve.
Any help would be greatly appreciated

Be careful that the correct method is getPortraitURL(ThemeDisplay themeDisplay), URL is in uppercase.
Try this:
$user.getPortraitURL($themeDisplay)
You should also be able to use $themeDisplay.
Access Objects from Velocity is useful link for all liferay velocity variables.

The variable you need to use for ThemeDisplay is
$theme_display

Related

How to retrieve NavItem in liferay freemarker base theme in liferay 7?

i had used below code but it is not working.
<#assign navItem = objectUtil("com.liferay.portal.kernel.theme.NavItem") />
it give below error.
Caused by: freemarker.core._TemplateModelException: Java constructor "com.liferay.portal.kernel.theme.NavItem.com.liferay.portal.kernel.theme.NavItem(javax.servlet.http.HttpServletRequest, com.liferay.portal.kernel.model.Layout, Map)" takes 3 arguments, but 0 was given.__----_FTL stack trace ("~" means nesting-related)
I had also used below code it is also not working.
<#assign navItemClass = portal.getClass().forName("com.liferay.portal.kernel.theme.NavItem")>
Basically i want to retrieve NavItem object in theme and want to use it.
The scripting context gets a large part of its variables injected by TemplateContextHelper. In there you can find several relevant values for the underlying problem that you describe in the comment to your question:
layout is representing the current page (layout is the technical name)
layouts is a collection of all pages
the same values can be retrieved through themeDisplay
navItems is a collection of all navItems, but you'll have to find the one relating to the current page yourself. It might be easier to go through layout
Browsing through the TemplateContextHelper sourcecode might give you the hints you need.

Why aren't Liferay objects rendering in my Velocity template?

A simple example:
/* Get user roles */
#set($userId=$request.attributes.get('USER_ID'))
#set($roleLocalService=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))
$roleLocalService.getUserRoles($userId)
What renders on the page is just the text with no data.
$roleLocalService.getUserRoles($userId)
What am I missing?
Be sure you're allowed to use serviceLocator. The default value in portal.properties is velocity.engine.restricted.variables=serviceLocator which means serviceLocator is not available to templates. Set it to "blank" (or at least do not include serviceLocator). For example, set it to
velocity.engine.restricted.variables=
in a portal-ext.properties file inside the Liferay Home directory.
$request.attributes.get is going to give you the String value of the userId. So you need to convert this to a Long using something like:
$roleLocalService.getUserRoles($getterUtil.getLong($userId))

How to open JSF page and pass parameter by href link?

Now I have one website. I want to provide user one link with one parameter. Then he can access the website and query something according the parameter. I want to the link is "http://10.182.20.86:8080/webproject/main.xhtml/scenarioid=1234" etc.
Now I use rest to pass the parameter to Java. But I can't get instance from FacesContext. It always returns null, when I use FacesContext.getCurrentInstance() in Java. Do you know how to implement the function by link with parameter in JSF? Thanks.
I have solved it by create one class and it extends HttpServlet. I get the parameter by request and get bean attribute by HttpServletRequest getSession. Now it works. Thanks, Balus.

How to put catche in liferay?

Earlier we are putting catche in liferay with help of below code.
MultiVMKeyPoolUtil.put("SCHOOL", "ID", "Files");
In Liferay 6.2 MultiVMKeyPoolUtil not available so how can we put catche.
There are no method like put in MultiVMPoolUtil class. I had search many alternative but couldn't find put method in it.
As MultiVMKeyPoolUtil not supported by liferay 6.2. I had used MultiVMPoolUtil class like below.
MultiVMPoolUtil.getCache("SCHOOL").put("ID","Files");
If you want to put collection as cache use below code.We require to make list serializable
MultiVMPoolUtil.getCache("SCHOOL").put("ID",(Serializable) courses);

Liferay 6.1 dynamic data list in velocity

I build some custom views for DDL. For text type fields i'd iterate through records and display just like below:
$record.getField("field_name").getValue()
This won't work for the date field. I presume I should use getRenderedValue(themeDisplay) method instead of getValue()
I can access themeDisplay in velocity markup through request.get("theme-display"),
however if i set it as variable and pass as argument to getRenderedValue method I get no result
Is this the right way to do it? Whats the best practice to obtain themeDisplay in velocity markup?
I think it should work when you iterate through the records. I presume, before iterating you have done
$serviceLocator.findService("com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService"))
#set ($recordSetId = $getterUtil.getLong($reserved_record_set_id.data))
#set ($records = ${ddlRecordsUtil.getRecords($recordSetId)})
1st Check: Can you print records? If No, Can you please check that you have added this property in your portal-ext.properties file?
journal.template.velocity.restricted.variables=
By default it will be
journal.template.velocity.restricted.variables=serviceLocator
you need to remove serviceLocator and then try?
HTH

Resources