Kiran Babu
Kiran Babu

Reputation: 77

Method to display list of available portlets in a portlet

Generally liferay has ADD option for displaying available portlets.

I want them to appear in a drop-down and that should be in a custom plugin-portlet, so I am searching in API which method is retrieving the available portlets, but I didn't find any.

Please help me in this as I am stuck with this and also on selecting from the drop-down the portlet should be added to the page.

Upvotes: 1

Views: 788

Answers (1)

Olaf Kock
Olaf Kock

Reputation: 48057

The "Add...More" dialog is displayed by the dockbar portlet. You can find the implementation of the UI part of this in Liferay's source in portal-web/docroot/html/portlet/dockbar/add_panel.jsp, which also includes view_category.jsp in the same directory.

While this jsp code is not the prettiest, you'll easily find that PortletLocalService is the one where you find the relevant information, together with an actual sample of how to access the list of portlets by category, sort them according to the current user's locale etc.

As you're asking for more concrete pointers: In add_panel.jsp you can find:

for (PortletCategory curPortletCategory : categories) {
    if (curPortletCategory.isHidden()) {
        continue;
    }
    request.setAttribute(WebKeys.PORTLET_CATEGORY, curPortletCategory);
    request.setAttribute(WebKeys.PORTLET_CATEGORY_INDEX, String.valueOf(portletCategoryIndex));
    %>
    <liferay-util:include page="/html/portlet/dockbar/view_category.jsp" />
    <%
    portletCategoryIndex++;
}
%>

and some excerpts from view_category.jsp:

<%
PortletCategory portletCategory = (PortletCategory)request.getAttribute(WebKeys.PORTLET_CATEGORY);
int portletCategoryIndex = GetterUtil.getInteger((String)request.getAttribute(WebKeys.PORTLET_CATEGORY_INDEX));
// ...
Set<String> portletIds = portletCategory.getPortletIds();
// ...
for (String portletId : portletIds) {
    Portlet portlet = PortletLocalServiceUtil.getPortletById(user.getCompanyId(), portletId);

    if ((portlet != null) && PortletPermissionUtil.contains(permissionChecker, layout, portlet, ActionKeys.ADD_TO_PAGE)) {
        portlets.add(portlet);
        // ... and so on

Hope this excerpt helps. See the rest of the file for what you can actually do with the resulting list. Also, Portlet's interface might help if you need more details.

Upvotes: 1

Related Questions