Wednesday, February 20, 2013

Getting access to menu from portlet in liferay


The below code describes the way of accessing menu items from the portlet. Here i have called it from jsp. The best thing about this is you can use the beautiful permission management of menu items of liferay as well without bothering about that. Writing services for fetching menu names from the layout tables will need more codes for permission managements......

Hope this helps.........


<%--
/**
* Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
--%>
<%@ page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@ page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="java.util.*"%>
<%@page import="com.liferay.portal.model.Layout"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ page import="com.liferay.portal.theme.NavItem" %>
<%@ page import="com.liferay.portal.theme.RequestVars" %>



<portlet:defineObjects />
<liferay-theme:defineObjects />

<div style="width:100%">


<%
//ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String title = themeDisplay.getLayout().getName(themeDisplay.getLocale());

%>

<% 
List<NavItem> navItems = new ArrayList<NavItem>();
if (layout != null) {
RequestVars requestVars = new RequestVars(request, themeDisplay, layout.getAncestorPlid(), layout.getAncestorLayoutId()); 
navItems = NavItem.fromLayouts(requestVars, layouts);
}
%>

<%for (NavItem navItem : navItems){
if (navItem.getName().equalsIgnoreCase(title)){%>


<%if (navItem.hasChildren()){%>

<%
for(NavItem navChild : navItem.getChildren()){
%>

 <div style="float:left;" class="newsMenuPortlet">
<a href="<%= navChild.getURL() %>" <%= navChild.getTarget() %>><%= navChild.getName() %></a>
</div>
<%}%>

<%}%>

<%}%>

<%}%>







</div>

No comments:

Post a Comment