4

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..

Abhishek Jain
  • 236
  • 2
  • 18

2 Answers2

3

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:

enter image description here

This is just the basic idea — the documentation is very instructive about it.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • The portlet must be not instanceable. Please change it to false in order the portlet gets displayed in control panel change "com.liferay.portlet.instanceable=true" to "com.liferay.portlet.instanceable=false", – suatCoskun Feb 13 '18 at 15:06
  • @suatCoskun Updated. Thanks! (I thought my example used to work but It was some time ago so I have missed something...) – brandizzi Feb 14 '18 at 18:14
1

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

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26