Liferay 7 Hooks Tutorials

  • In Liferay Plugin SDK development, we use HOOK / EXT to customize liferay functionalities.
  • In Liferay DXP, technically there  is no HOOK/EXT concepts any more, but Liferay leverages OSGI platform to modify the core services,filters,indexes,JSP’,s Language properties.
  • As you all know, OSGI uses Service Registry modal that lookup service implementation at run time to identify the implementation.
  • Liferay takes OSGI framework as advantage to customize liferay functionality
    • OSGI Service Registry  – OSGI uses Service Registry concepts and provides service implementation at runtime.
    • Extender pattern will add extension code to already existing bundles via service modal.  Liferay’s Portlet Container and when a OSGI component has (service = Portlet.class) then it will add to the Portlet Container bundle). In the same way, ServiceWrapper, Filter, Struts Actions,PortalActions,Modal Listeners,Indexer will be customized.
    • Fragment Host:
      •  A fragment bundle will be created to modify the contents in core module(called as host bundle).
      • The resources (JSP pages in resource folder) defined in fragment host are replaced with resources in Host bundle.
      • Let’s take example:
        • Liferay login-web bundle   –  A Host bundle contains  login.jsp,forgot_password.jsp in resource folder
        • login-hook-web bundle:  it is fragment host bundle where resource defined in this bundle are replaced with login-web bundle. We need to add  fragment host entry in MANIFEST.MF file  to override the Host bundle resources.

Liferay follows same kind of  pattern to override  the core functionalities such as services,modal listeners,filters,actions like below:

  1. Create Component class
  2. Add the service class name property in @Component annotation
    1. @Component(
       property = {for language,filters }, 
       service = {BaseClass.class
      )
  3. Component class is required to extend/implement corresponding class/interface. We will see complete tutorials in the following sections.

Liferay 7 JSP HOOK:

Liferay Uses OSGI fragment concepts to override liferay module JSP’s.

  • Create Module
  • Add Fragment-Host:{bundle-name}
    1. Manifest-Version: 1.0
      Bnd-LastModified: 1483624888998
      Bundle-ManifestVersion: 2
      Bundle-Name: org.javasavvy.demo.hooks
      Bundle-SymbolicName: org.javasavvy.demo.hooks
      Bundle-Version: 1.0.0
      Created-By: 1.8.0_101 (Oracle Corporation)
      Fragment-Host: com.liferay.login.web;bundle-version="1.1.3"
  • copy the JSP page from Host bundle(Actual bundle) to Fragmented Bundle
  • Click here to access for complete tutorial: Liferay 7 JSP Hook tutorial

 

Liferay 7 Language Properties Hook Configuration:

  • Create Component class
  • add the below configuration in to @Component annotation
    1. @Component(
       property = { "language.id=en_US" }, 
       service = ResourceBundle.class
      )
  • Class Need to extend ResourceBundle
  • Click here to access Liferay 7 Language Hook.

Liferay 7 Services Hook Configuration:

  • Create component class
  • Add the below annotation config in @Component annotations
    • @Component(
       immediate=true,
       service = ServiceWrapper.class
        )
  • Class need to extend the {entity}ServiceWrapper.java (UserServiceWrapper,JournalArticalServiceWrapper) and override the required methods
  • Click here to access Liferay 7 Service Customization Hook tutorial

Liferay 7 Struts action Hook Configuration:

  • Create component class
  • Add the below annotation config in @Component annotations
    • @Component(
       immediate=true,
       property={
       "path={struts-action-path}"
       },
       service = StrutsAction.class
      )
  • Class need to extend the BaseStrutsAction class and override the execute method
  • Click here to access Liferay 7 Struts Action tutorial

Liferay 7 Action Commands Hook Configuration:

  • Create component class
  • Add the below annotation config in @Component annotations
    • @Component(
       immediate = true,
       property = {
       "javax.portlet.name=com_liferay_login_web_portlet_LoginPortlet",
       "mvc.command.name=/login/login",
       "service.ranking:Integer=100"
       },
       service = MVCActionCommand.class
       )
  • service.ranking:Integer is mandatory to override action command
  • Class need to extend the class and override the doProcessAction or ProcessAction methods
  • Click here to access Liferay 7 Action command Tutorial

Liferay 7 Modal Listener Hook Configuration:

Liferay 7 Filter Hook Configuration:

  • Create component class
  • Add the below annotation config in @Component annotations and update the url paht in “url-pattern”:
    • @Component(
       immediate = true, 
       property = {
       "servlet-context-name=", 
       "servlet-filter-name=Custom Filter",
       "url-pattern=/*" 
       }, 
       service = Filter.class
       )
  • Class need to extend the BaseFilter class and override the required method: processFilter()
  • Click here to access Liferay 7 Filter Hook Tutorial

 

Hope this helps and Click here to access complete Hook Tutorials along with code.

 

2 thoughts on “Liferay 7 Hooks Tutorials”

Comments are closed.