Making portlet a PanelApp
Or how to include a portlet into the Control Panel
Overview
You may need to implement a portlet, which should be accessible only by Administrator. In this case it makes sense to include it into the Control Panel, and prevent it from being added to the page. This article will explain how to achieve this.
Implementation
1. Implement a portlet class:
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=" + ProjectsAdminPortletKeys.DISPLAY_CATEGORY,
"com.liferay.portlet.instanceable=true",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + ProjectsAdminPortletKeys.PORTLET_ID,
"javax.portlet.display-name=" + ProjectsAdminPortletKeys.DISPLAY_NAME,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user",
"com.liferay.portlet.add-default-resource=true",
"com.liferay.portlet.layout-cacheable=true",
"com.liferay.portlet.private-request-attributes=false",
"com.liferay.portlet.private-session-attributes=false",
"com.liferay.portlet.render-weight=50",
"com.liferay.portlet.use-default-template=true",
"javax.portlet.expiration-cache=0",
"com.liferay.portlet.css-class-wrapper=" + ProjectsAdminPortletKeys.CSS_CLASS_WRAPPER,
"com.liferay.portlet.header-portlet-css=/css/main.css"
},
service = Portlet.class
)
public class ProjectsAdminPortlet extends MVCPortlet {
}
}
2. Implement a panel app class:
@Component(
immediate = true,
property = {
"panel.app.order:Integer=0",
"panel.category.key=" + PanelCategoryKeys.SITE_ADMINISTRATION_CONTENT
},
service = PanelApp.class
)
public class ProjectsAdminPanelApp extends BasePanelApp {
@Override
public String getPortletId() {
return ProjectsAdminPortletKeys.PORTLET_ID;
}
@Override
@Reference(
target = "(javax.portlet.name=" + ProjectsAdminPortletKeys.PORTLET_ID + ")",
unbind = "-"
)
public void setPortlet(Portlet portlet) {
super.setPortlet(portlet);
}
}
"panel.category.key” - defines the category in Control Panel, where the portlet should be added to;
"panel.app.order” - defines the portlet position in the portlet list.
3. Constants class:
public class ProjectsAdminPortletKeys {
public static final String DISPLAY_NAME = "Projects";
public static final String DISPLAY_CATEGORY = "category.hidden";
public static final String PORTLET_ID = "com_sample_project_web_portlet_ProjectsAdminPortlet";
public static final String CSS_CLASS_WRAPPER = "projects-admin";
}
Note: category should be hidden (“category.hidden”) to prevent the portlet from being added to a portal page.
Enjoy 😏
Comments
Post a Comment