Alimozzaman

Liferay: Multiple themes selection

Posted on: February 2, 2011

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. I hope this will help selecting theme from UI level in very convenient way.

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 ($my_places_url1 = $portletURLFactory.create($request, “49”, $page.getPlid(), “ACTION_PHASE”))

$my_places_url1.setWindowState(“normal”)

$my_places_url1.setPortletMode(“view”)

$my_places_url1.setParameter(“struts_action”, “/my_places/themes”)

// Set the theme ID

$my_places_url1.setParameter(“themeId”, “classic”)

<span>

<li>   <a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” ><img src=”$images_folder/icon/stp-oranssi.png” alt=”Oranssi” /></a>

</li>

// set the second theme ID

$my_places_url1.setParameter(“themeId”, “classic_green “)

 

 

 

<li>

<a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” ><img src=”$images_folder/icon/stp-red.png” alt=”Stp” /> </a>

</li>

//Set  the third theme ID

$my_places_url1.setParameter(“themeId”, “custom_theme_ID”)

<li>

<a  class = “theme-update-link” href=”#” onclick=”updateTheme(‘$my_places_url1.toString()’,this)” > <img src=”$images_folder/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 me an email if u need further help  and also make valuable comment to make it much more clear!!

 

 

 

Advertisements

17 Responses to "Liferay: Multiple themes selection"

[…] this is not made in LR. But if you want something like this take a look at Alimozzaman blog (here). He wrote a step by step instruction how to do it. I think he should fill JIRA ticket for that and […]

Hello there,
Can u please tell the ticket number related to this?

[…] this webpage can give you a hint. Flag Please sign in to flag this as […]

“Liferay: Multiple themes selection Alimozzaman” was in fact a excellent blog post.
In case it possessed a lot more photographs it would likely be perhaps even more beneficial.

Regards ,Lyda

It was more like a programming , but if I find some good picture to share , definitely will do so.

I want to express appreciation to the writer just for rescuing me from this
particular challenge. Just after researching through the internet and obtaining concepts which are not pleasant, I assumed my life was over.
Existing without the presence of solutions to the issues you’ve resolved by way of your main blog post is a serious case, and the kind that might have in a negative way damaged my entire career if I hadn’t encountered your web page.
Your personal knowledge and kindness in playing with a lot of
stuff was valuable. I am not sure what I would’ve done if I had not encountered such a solution like this. I can at this moment relish my future. Thank you so much for this professional and result oriented help. I will not think twice to recommend your web page to any individual who would need guidance about this matter.

Thanks a lot! If you feel, there is any Liferay related topic where I can write , pls share with me.

Hello would you mind sharing which blog platform you’re using? I’m planning to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.

P.S Apologies for being off-topic but I had to ask!

Sorry for being late! WordPress blog is cool, so you might use it!

This is the perfect blog for everyone who really wants to understand this topic.

You understand so much its almost tough to argue with you (not
that I personally would want to…HaHa). You definitely put a new
spin on a subject that has been written about for decades.
Excellent stuff, just wonderful!

Thanks a lot, share me some topics so that I can pen on those too!

You are so interesting! I don’t suppose I’ve read anything like this before.
So great to find someone with a few unique thoughts on this topic.
Seriously.. thanks for starting this up. This
site is one thing that’s needed on the web, someone with some originality!

Thanks a lot for your complement and i will try my best to help community!

Hey I am so happy I found your blog page, I really found
you by error, while I was researching on Aol for something
else, Regardless I am here now and would just like to say thank you for a fantastic post and a all round exciting blog
(I also love the theme/design), I don’t have time to read through it all at the moment but I have book-marked it and also added in your RSS
feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic work.

Thanks! Its my pleasure 🙂

Thanks! This is definitely an fantastic online site.

This design is spectacular! You definitely know how
to keep a reader amused. Between your wit and your videos, I
was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
I really loved what you had to say, and more than that, how you presented it.
Too cool!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: