Reputation: 236
How can I add a custom portlet to the Control Panel section? I saw various tutorials but all are of liferay 6.2. How to accomplish it in liferay 7? Thanx in advance..
Upvotes: 4
Views: 3273
Reputation: 27090
In Liferay 7, if you are using bundles (for example, created via the Blade tools), you can get it working with panel apps. A panel app maps a portlet to a position in the Control Panel.
Suppose you have a portlet generated by Blade, like the one below:
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=false",
"javax.portlet.name=cpportlet",
"javax.portlet.display-name=Control Panel Portlet",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class CpPortlet extends GenericPortlet {
@Override
protected void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
PrintWriter printWriter = renderResponse.getWriter();
printWriter.print("cp-portlet Portlet - Hello World!");
}
}
Now you just create another OSGi component implementing the PanelApp
service:
@Component(
immediate = true,
property = {
"panel.app.order:Integer=10000", // Defines position in list
"panel.category.key=" + PanelCategoryKeys.SITE_ADMINISTRATION_CONTENT // To appear in the "Content" session
},
service = PanelApp.class
)
public class CpPanelApp extends BasePanelApp {
@Override
public String getPortletId() {
return "cpportlet"; // Same name of the portlet.
}
@Override
@Reference(
target = "(javax.portlet.name=cpportlet)",
unbind = "-"
)
public void setPortlet(Portlet portlet) {
super.setPortlet(portlet);
}
}
To compile that, you will depend on the "Application List app" API - It is there that we find the PanelApp
class. So, just add this dependency to your build.gradle
, as below:
dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
compileOnly group: "org.osgi", name: "org.osgi.compendium", version: "5.0.0"
compileOnly group: "com.liferay", name: "com.liferay.application.list.api", version: "2.0.0" // Dependency added
}
Now deploy it and the portlet will appear in the listing:
This is just the basic idea — the documentation is very instructive about it.
Upvotes: 3
Reputation: 4210
You can define control-panel category by properties for Component:
com.liferay.portlet.control-panel-entry-category=<String>
com.liferay.portlet.control-panel-entry-weight=<double>
Please refer mapping listed here: https://dev.liferay.com/develop/reference/-/knowledge_base/7-0/portlet-descriptor-to-osgi-service-property-map
Upvotes: 1