Accessing ThemeDisplay Information

The Liferay global JavaScript Object exposes useful methods, objects, and properties, each containing a wealth of information, one of which is ThemeDisplay. If you have experience with Java development in Liferay DXP, you may be familiar with ThemeDisplay. The JavaScript object exposes the same information as the ThemeDisplay Java Class. It gives you access to valuable information that you can use in your applications, such as the Portal instance, the current user, the user’s language, whether the user is signed in or being impersonated, the file path to the theme’s resources, and much more.

The Liferay global object is automatically available in Liferay DXP at runtime. To access the ThemeDisplay object, use the following dot notation in your app:

Liferay.ThemeDisplay.method-name

This reference describes some of the most commonly used ThemeDisplay methods for retrieving IDs, file paths, and login information. An exhaustive list of all of the available methods is displayed in the table below:

MethodTypeDescription
getLayoutIdnumber
getLayoutRelativeURLstringReturns the relative URL for the page
getLayoutURLstring
getParentLayoutIdnumber
isControlPanelboolean
isPrivateLayoutboolean
isVirtualLayoutboolean
getBCP47LanguageIdnumber
getCDNBaseURLstringReturns the content delivery network (CDN) base URL, or the current portal URL if the CDN base URL is null
getCDNDynamicResourcesHoststringReturns the content delivery network (CDN) dynamic resources host, or the current portal URL if the CDN dynamic resources host is null
getCDNHoststring
getCompanyGroupIdnumber
getCompanyIdnumberReturns the portal instance ID
getDefaultLanguageIdnumber
getDoAsUserIdEncodedstring
getLanguageIdnumberReturns the user’s language ID
getParentGroupIdnumber
getPathContextstring
getPathImagestringReturns the relative path of the portlet’s image directory
getPathJavaScriptstringReturns the relative path of the directory containing the portlet’s JavaScript source files
getPathMainstringReturns the path of the portal instance’s main directory
getPathThemeImagesstringReturns the path of the current theme’s image directory
getPathThemeRootstringReturns the relative path of the current theme’s root directory
getPlidstringReturns the primary key of the page
getPortalURLstringReturns the portal instance’s base URL
getScopeGroupIdnumberReturns the ID of the scoped or sub-scoped active group (e.g. site)
getScopeGroupIdOrLiveGroupIdnumber
getSessionIdnumberReturns the session ID, or a blank string if the session ID is not available to the application
getSiteGroupIdnumber
getURLControlPanelstring
getURLHomestring
getUserIdnumberReturns the ID of the user for which the current request is being handled
getUserNamestringReturns the user’s name
isAddSessionIdToURLboolean
isFreeformLayoutboolean
isImpersonatedbooleanReturns true if the current user is being impersonated. Authorized administrative users can impersonate act as another user to test that user’s account
isSignedInbooleanReturns true if the user is logged in to the portal
isStateExclusiveboolean
isStateMaximizedboolean
isStatePopUpboolean

The example configuration below alerts users with a standard message if they are a guest or a personal greeting if they are signed in. This is a basic example, and perhaps a bit invasive, but it illustrates how you can create unique experiences for each user with the ThemeDisplay APIs:

if(Liferay.ThemeDisplay.isSignedIn()){
    alert('Hello ' + Liferay.ThemeDisplay.getUserName() + '. Welcome Back.')
}
else {
    alert('Hello Guest.')
}
« Liferay JavaScript APIsWorking with URLs in JavaScript »
この記事は役に立ちましたか?
2人中1人がこの記事が役に立ったと言っています