How to select Multiple themes in Liferay
Multiple themes selection
We can select the whole site theme from any navagation of our site which will be different user to user. Say, you select your own theme that would be different than other users in a multi user site where user register to use the site.
1. Define a structs path in which you will be adding your Action class.
For example – I made a path using an existing portlet.
<action path=”/my_places/themes” type=”com.example.actions.themes.ThemeSwitchAction”> <forward name=”portlet.my_places.view” path=”portlet.my_places.view” />
</action>
2. Now generate the url into your VM file, in my case I did add in dock.vm file like the following way.
#set ( = .create(, “49″, .getPlid(), “ACTION_PHASE”))
.setWindowState(“normal”)
.setPortletMode(“view”)
.setParameter(“struts_action”, “/my_places/themes”)
// Set the theme ID
.setParameter(“themeId”, “classic”)
<span>
<li> <a class = “theme-update-link” href=”#” onclick=”updateTheme(‘.toString()’,this)” ><img src=”/icon/stp-oranssi.png” alt=”Oranssi” /></a>
</li>
// set the second theme ID
.setParameter(“themeId”, “classic_green “)
<li>
<a class = “theme-update-link” href=”#” onclick=”updateTheme(‘.toString()’,this)” ><img src=”/icon/stp-red.png” alt=”Stp” /> </a>
</li>
//Set the third theme ID
.setParameter(“themeId”, “custom_theme_ID”)
<li>
<a class = “theme-update-link” href=”#” onclick=”updateTheme(‘.toString()’,this)” > <img src=”/icon/stp-green.png” alt=”Classic” /> </a>
</li>
</span>
//Add the following Javascript to post your data.
<script type=”text/javascript”>
function updateTheme(url,obj){
var targetURL = url;
jQuery.post(targetURL, {ajax: ‘true’ },
function(){
location.reload();
});
}
</script>
3. Now you add the following lines in your Action class.
public void processAction(ActionMapping mapping, ActionForm form,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse) throws Exception {
_log.info(ThemeSwitchAction.class.getName() + “ WORKED”);
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest
.getAttribute(WebKeys.THEME_DISPLAY);
String themeId = ParamUtil.getString(actionRequest, “themeId”);
Group group = GroupLocalServiceUtil.getGroup(themeDisplay.getLayout()
.getGroupId());
// Setting group theme.
LayoutSetLocalServiceUtil.updateLookAndFeel(group.getGroupId(),
themeId, “01″, “”, false);
// Setting user home page theme.
LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getUser()
.getGroup().getGroupId(), themeId, “01″, “”, false);
_log.info(ThemeSwitchAction.class.getName()
+ “ set the theme for you.”);}
4. To make sure your themeID is update every where , you can add this method into service preaction in your hook
protected void updateGroupThemes(HttpServletRequest request,
HttpServletResponse response) {
try {
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
LayoutSet userHomeLayout = LayoutSetLocalServiceUtil.getLayoutSet(
themeDisplay.getUser().getGroup().getGroupId(), false);
String themeId = userHomeLayout.getThemeId();
LayoutSet updatedLayout = LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getLayout().getGroupId(), false, themeId, “01″, “”, false);
LayoutSetLocalServiceUtil.updateLayoutSet(updatedLayout);
} catch (Exception e) {
//
}
}
5. Also you can add the follwoing lines into your community update method if u need to update your community layout theme on run time.
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
LayoutSet userHomeLayout = LayoutSetLocalServiceUtil.getLayoutSet(
themeDisplay.getUser().getGroup().getGroupId(), false);
String themeId = userHomeLayout.getThemeId();
LayoutSet updatedLayout = LayoutSetLocalServiceUtil.updateLookAndFeel(themeDisplay.getLayout().getGroupId(), false, themeId, “01″, “”, false);
LayoutSetLocalServiceUtil.updateLayoutSet(updatedLayout);
Enjoying making default theme according to user!!!
Drop us an email if u need further help and also make valuable comment to make it much more clear!!