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 tutorial 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 at the end of this tutorial.

Retrieving IDs

The methods below retrieve various Portal elements related to the current user:

getCompanyId: Returns the company ID.

getLanguageId: Returns the user’s language ID.

getScopeGroupId: Returns the group ID of the current site.

getUserId: Returns the user’s ID.

getUserName: Returns the user’s name.

Next you can learn how to access file paths for various deployed entities.

Retrieving File Paths

The methods below retrieve file paths for various theme resources:

getPathImage: Returns the relative path of the portlet’s image directory.

getPathJavaScript: Returns the relative path of the directory containing the portlet’s JavaScript source files.

getPathMain: Returns the path of the portal instance’s main directory.

getPathThemeImages: Returns the path of the current theme’s image directory.

getPathThemeRoot: Returns the relative path of the current theme’s root directory.

Next you can learn how to retrieve information for the current user.

Retrieving Login Information

The methods below return a boolean value indicating whether the current user is signed in or being impersonated:

isImpersonated: Returns true if the current user is being impersonated. Authorized administrative users can impersonate act as another user to test that user’s account.

isSignedIn: Returns true if the user is logged in to the portal.

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 ThemeDisplay Methods

A complete list of the available Liferay.ThemeDisplay methods is shown 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

Liferay DXP JavaScript Utilities

« Introduction to Liferay JavaScript APIsWorking with URLs in JavaScript »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています