Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Bryancan Cantwell
List all portlets on a layout
December 18, 2007 9:43 AM
Answer

Bryancan Cantwell

Rank: Junior Member

Posts: 26

Join Date: June 18, 2006

Recent Posts

Is there a method available that can list portlets that are on a particular layout?
I'd like to modify the Sitemap portlet to not only show the tabs but the portlets on each tab in the tree so they can know what is on each tab and navigate to that tab (and possibly focus on the portlet they clicked).

This is a show stopper, so a fast reply is appreciated!
Thx,
Bryancan
Alex Wallace
RE: List all portlets on a layout
December 18, 2007 9:57 AM
Answer

Alex Wallace

Rank: Liferay Master

Posts: 640

Join Date: November 4, 2007

Recent Posts

Yes there is!

1
2Layout layout = LayoutLocalServiceUtil.getLayout(plid);
3LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
4List actualPortletList = layoutTypePortlet.getPortletIds();


You can get the plid from ThemeDisplay (an attribute named 'THEME_DISPLAY' in portlet request).... Of course, you can get the whole layout from ThemeDisplay too (.getLayout() and .getLayout().getPlid())

Hope this helps!
Bryancan Cantwell
RE: List all portlets on a layout
December 18, 2007 10:42 AM
Answer

Bryancan Cantwell

Rank: Junior Member

Posts: 26

Join Date: June 18, 2006

Recent Posts

OK, thx a ton.
I am 99% there I think I have a List now of the portlets on a page.
Now how can I get the proper name of that portlet for display in my sitemap tree?
And I have the url for the specific layout, I assume I can simply append the portlet id or name to the layouturl and it will change to that layout and focus on that portlet?

Thx,
Bryancan
Alex Wallace
RE: List all portlets on a layout
December 18, 2007 1:44 PM
Answer

Alex Wallace

Rank: Liferay Master

Posts: 640

Join Date: November 4, 2007

Recent Posts

For the display name.... use LanguageUtil.get ... the key to portlets are "javax.portlet.title."+ portletId


For focusing on particular portlets, you pass an anchor in the url like this

#p_PORTLET_ID

replace PORTLET_ID with your portletId...

hope this helps!
Bryancan Cantwell
RE: List all portlets on a layout
December 19, 2007 9:47 AM
Answer

Bryancan Cantwell

Rank: Junior Member

Posts: 26

Join Date: June 18, 2006

Recent Posts

Now I'm 99.999% there!
I have mixed results in what I get for portlet titles:

Analog Clock
Dictionary
javax.portlet.title.53_INSTANCE_z1Dg
javax.portlet.title.50
javax.portlet.title.48_INSTANCE_4zuW
javax.portlet.title.48_INSTANCE_WnzD
Reports
javax.portlet.title.66_INSTANCE_gBcr
Software Catalog
javax.portlet.title.47
Network Utilities
Password Generator
Countdown
Language
Maps
CSZ Search

Some are right some are not showing up like I want. Here is the code I have:
 1
 2<%!
 3private void _buildSiteMap(List layouts, int displayDepth, int curDepth, ThemeDisplay themeDisplay, StringMaker sm) throws Exception {
 4    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
 5
 6    sm.append("<ul style=\"padding-left: 25px\">");
 7
 8    for (int i = 0; i < layouts.size(); i++) {
 9        Layout layout = (Layout)layouts.get(i);
10
11        if (!layout.isHidden() && LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.VIEW)) {
12            String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
13            String target = PortalUtil.getLayoutTarget(layout);
14            
15            
16            LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
17            List actualPortletList = layoutTypePortlet.getPortletIds();
18
19            
20            
21
22            sm.append("<li>");
23            sm.append("<a href=\"");
24            sm.append(layoutURL);
25            sm.append("\" ");
26            sm.append(target);
27            sm.append("> ");
28            sm.append(layout.getName(themeDisplay.getLocale()));
29            sm.append("</a>");
30            sm.append("<ul style=\"padding-left: 25px\">");
31            for (int l = 0; l < actualPortletList.size(); l++)
32            {
33                sm.append("<li>");
34                sm.append("<a href=\"");
35                sm.append(layoutURL+"#p_"+actualPortletList.get(l));
36                sm.append("\" ");
37                sm.append(target);
38                sm.append("> ");
39                sm.append(LanguageUtil.get(themeDisplay.getLocale(), "javax.portlet.title."+actualPortletList.get(l)));
40                sm.append("</a>");
41                sm.append("</li>");
42            }
43            sm.append("</ul>");
44            sm.append("</li>");
45
46            if ((displayDepth == 0) || (displayDepth > curDepth)) {
47                _buildSiteMap(layout.getChildren(), displayDepth, curDepth + 1, themeDisplay, sm);
48            }
49        }
50    }
51
52    sm.append("</ul>");
53}
54%>


These are all just the portlets that come with Liferay in a plain install...
Ray Augé
Re: [Liferay Forums][3. Development]RE: List all portlets on a layout
December 19, 2007 2:45 PM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

Strip away the _INSTANCE_#### and those should be fixed... (those are
simply "instances" of portlet javax.portlet.title.##)

Note sure why 47 (hello_world) isn't showing up...

Have a look at the code in
portal-web/docroot/html/portlet/layout_configuration/view_category.jsp to see how to do it "resolve the portlet name" the way it's done in the "Add Content" menu.
Bryancan Cantwell
RE: Re: [Liferay Forums][3. Development]RE: List all portlets on a layout
December 27, 2007 12:53 PM
Answer

Bryancan Cantwell

Rank: Junior Member

Posts: 26

Join Date: June 18, 2006

Recent Posts

OK, that worked great. However, now I have a different requirement. If a user changes the name of a particular portlet instance on a page I need to show thier custom name from the preferences in my new sitemap... Is there a method that by useing the portlet id that I get from the layout, I can go in and get the name that is in thier preferences for that particular instance?

Thx, Bryancan
Ray Augé
RE: Re: [Liferay Forums][3. Development]RE: List all portlets on a layout
December 28, 2007 6:56 AM
Answer

Ray Augé

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1195

Join Date: February 7, 2005

Recent Posts

hmm, if you consider that the portlet name change is stored in the
portlet preferences... which means you'd have to load all the portlet
preferences (for the portlet instances on a given page) and then get the
names... and then coerce those into your full list... it's doable I
think.
Bryancan Cantwell
RE: Re: [Liferay Forums][3. Development]RE: List all portlets on a layout
December 28, 2007 12:10 PM
Answer

Bryancan Cantwell

Rank: Junior Member

Posts: 26

Join Date: June 18, 2006

Recent Posts

There are a number of gePortletPreferences() methods... I just need to find the one I can access with what info I have and then look in that list for the existance of name and then use it if it is there or just use display name form the portlet object if not...
Here is what I have so far that works fine, can ne1 see how I can implement one of the portletpreferences abilities here:

 1
 2
 3<%@ include file="/html/portlet/site_map/init.jsp" %>
 4<%
 5List rootLayouts = null;
 6
 7
 8if (rootPlid > 0) {
 9    Layout rootLayout = LayoutLocalServiceUtil.getLayout(rootPlid);
10
11    rootLayouts = rootLayout.getChildren();
12}
13else {
14    rootLayouts = LayoutLocalServiceUtil.getLayouts(layout.getGroupId(), layout.isPrivateLayout(), LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
15}
16
17StringMaker sm = new StringMaker();
18
19_buildSiteMap(rootLayouts, displayDepth, 1, themeDisplay, sm);
20%>
21
22<%= sm.toString() %>
23
24<%!
25private void _buildSiteMap(List layouts, int displayDepth, int curDepth, ThemeDisplay themeDisplay, StringMaker sm) throws Exception {
26    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
27
28    sm.append("<ul>");
29    String p=null;
30    int a=0;
31    String pID = null;
32
33    for (int i = 0; i < layouts.size(); i++) {
34        Layout layout = (Layout)layouts.get(i);
35
36        if (!layout.isHidden() && LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.VIEW)) {
37            String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
38            String target = PortalUtil.getLayoutTarget(layout);          
39            
40            LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
41            List actualPortletList = layoutTypePortlet.getPortlets();            
42
43            sm.append("<li>");
44            sm.append("<a href=\"");
45            sm.append(layoutURL);
46            sm.append("\" ");
47            sm.append(target);
48            sm.append("> ");
49            sm.append(layout.getName(themeDisplay.getLocale()));
50            sm.append("</a>");
51            sm.append("<ul>");
52            for (int l = 0; l < actualPortletList.size(); l++)
53            {
54                Portlet portlet = (Portlet)actualPortletList.get(l);
55                p = portlet.getDisplayName().toString();
56                              
57                sm.append("<li>");
58                sm.append("<a href=\"");
59                sm.append(layoutURL+"#p_"+portlet.getPortletId());
60                sm.append("\" ");
61                sm.append(target);
62                sm.append("> ");
63                sm.append(p);//LanguageUtil.get(themeDisplay.getLocale(), "javax.portlet.title."+pID)
64                sm.append("</a>");
65                sm.append("</li>");
66            }
67            sm.append("</ul>");
68            sm.append("</li>");
69
70            //if ((displayDepth == 0) || (displayDepth > curDepth)) {
71            //    _buildSiteMap(layout.getChildren(), displayDepth, curDepth + 1, themeDisplay, sm);
72            //}
73        }
74    }
75
76    sm.append("</ul>");
77}
78%>
Pascal Wasem
RE: Re: [Liferay Forums][3. Development]RE: List all portlets on a layout
July 8, 2010 3:46 AM
Answer

Pascal Wasem

Rank: New Member

Posts: 17

Join Date: March 31, 2010

Recent Posts

Liferay 5.2.3
 1
 2<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
 3<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
 4<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
 5<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
 6<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
 7<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
 8
 9<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
10<%@ page import="com.liferay.portal.kernel.servlet.SessionMessages" %>
11<%@ page import="com.liferay.portal.kernel.servlet.SessionErrors" %>
12
13<%@ page import="java.util.List" %>
14
15<portlet:defineObjects />
16<liferay-theme:defineObjects />
17
18<%
19//List<Portlet> allPortlets = themeDisplay.getLayoutTypePortlet().getAllPortlets();
20//List<Portlet> portlets themeDisplay.getLayoutTypePortlet().getPortlets();
21List<String> portletIds = themeDisplay.getLayoutTypePortlet().getPortletIds();
22%>
23
24
25<ul>
26<%
27
28for (String pid : portletIds ) {
29
30    %>
31    <li><%= pid %></li>
32    <%
33}
34%>
35</ul>
36
37<input type="text" id="<portlet:namespace />Input" />
38<input type="button" id="<portlet:namespace />Button" value="Hide" />
39
40
41
42<script type="text/javascript">
43
44    jQuery(document).ready(function() {
45
46        jQuery('#<portlet:namespace />Button').bind("click",function(){
47
48            var pid = jQuery('#<portlet:namespace />Input').val();
49
50            alert('#portlet-wrapper-'+pid);
51            jQuery('#portlet-wrapper-'+pid).hide();
52
53        });
54
55    });
56
57</script>