Wednesday, 25 September 2013

Creating community (Site) specific JSP hook in Liferay 6.1


                    Liferay provides great flexibility to customize core features of Liferay. One of the common requirement is to customize user interface of built in portlets like blogs, announcement etc. We can do this by using JSP hooks plugin. Now it is often required to have different look and feel for same portlet in different communities or organizations. We often use if/else kind of conditionals in JSP hooks to do so. But this is not a clean approach. With Liferay 6.1, it is now possible to write JSP hooks specific to sites. This blog provides step by step guide to create community specific hooks.
Example Hook
Suppose we have a requirement where customer wants to have a title “Connect Sam” in the blogs portlet for connect-sam community (Site). All other sites should have default behavior. Here are the steps to implement this requirement using community (Site) specific hooks.

Step 1 Install Liferay IDE to your eclipse environment. (Please refer http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/installati-6 link to install Liferay IDE)
Step 2 Configure Liferay 6,1 plugin SDK and Liferay Portal server in Liferay IDE. (Please refer http://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/set–4 link to configure Liferay IDE)
Step 3 Create new Liferay project using File | New | Liferay Project menu item.
Step 4 In the new Liferay project dialog enter details as shown in following screenshot.
Step 5 Now add new Liferay Hook using File | New | Liferay Hook menu item. In the New Liferay Hook  dialog box select Custom JSPs hook type and press next.
Step 6 From the next view keep defaults and click on Add from Liferay… button and select /html/portlet/blogs/view.jsp from the file list. Click on finish button to create the hook.
Step 7 Now in view.jsp add 
<h1< span=”">>Connect Sam> line just after following line.
            <%@ include file=”/html/portlet/blogs/init.jsp” %>
Step 8 Now open liferay-hook.xml and add following line after <custom-jsp-dir> /custom_jsps< /custom-jsp-dir>
<custom-jsp-global> false</custom-jsp-global >
Step 9 Now deploy the hook plugin.
Step 10 After the deployment check if you can see “Connect Sam” heading in blogs portlet. Put this portlet on both guest and connect-sam community pages. Check if you can see the header. You will not see that as we added  false property in liferay hook configuration file. 
Step 11 Now go to connect-sam site and open control panel. In control panel select “Site Settings” menu item. Now in the site settings you will find one more option called Application Adapter.And in the drop down you will find the “Blogs Customization” hook display name. Select that as shown in bellow figure.


Step 11 Now re-test blogs portlet on connect-sam site and other sites. You will find our change is only coming in connect-sam site as we selected application adapter.  
Summary
With Liferay 6.1, we have now two types of JSP hooks. Global and Site specific. We can configure hook as site specific hook by adding <custom-jsp-global> tag in liferay-hook.xml file. By default <custom-jsp-global> will be true. If we create site specific hook by making <custom-jsp-global> to false, we can choose the hook as application adapter for selected sites. This saves complex hard coded logic to create site specific hook.

No comments:

Post a Comment