Liferay Hooks are plugins that let you customise some of the Portal functionality.
In this post we are creating a simple Liferay hook to change the value of a property in the portal.properties, override one of the Portal jsps and handling an event.
Not all portal.properties can be overridden, the liferay-hook_5_2_0.dtd describes the supported properties in a hook.
The hook plugin that we are building here is for Liferay 5.2 version. For 5.1 have a look at the DTD here
Create war
Create a new empty war project and include the liferay-plugin-package.properties:
change-log= module-incremental-version=1 tags=hook name=Example Hook module-group-id=usefulfor short-description=customise liferay portal to adapt it to my needs
The web.xml includes the HookContextListener:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>example-hook</display-name> <listener> <listener-class>com.liferay.portal.kernel.servlet.HookContextListener</listener-class> </listener> </web-app>?
In your WEB-INF directory create the file liferay-hook.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd"> <hook> <portal-properties>portal.properties</portal-properties> <custom-jsp-dir>/WEB-INF/jsp</custom-jsp-dir> </hook>
indicating the location of your portal.properties and also the path in your war where the portal will go to pick the overridden jsps.
Override the properties (the allowed ones
) with your values in the portal.properties, including the classes that will be called when the events are triggered:
# No terms of use terms.of.use.required=false # # Login event # login.events.pre=com.custom.hook.events.login.CustomPreLogin login.events.post=com.custom.hook.events.login.CustomPostLogin # # Logout event # logout.events.pre=com.custom..hook.events.logout.CustomPreLogout
Your events have to extends the abstract class com.liferay.portal.kernel.events.Action and implement the run method that will be called on the event happening:
import com.liferay.portal.kernel.events.Action; import com.liferay.portal.kernel.events.ActionException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CustomPreLogin extends Action { @Override public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException { //do something before the login } }
Every jsp in your specified custom-jsp-dir will be picked up to replace the relevant one in the portal as long as it has the same name and it has exactly the same path.
So if your jsp folder is in /WEB-INF/jsp and you want to override the portlet_messages.jspf jsp located in liferay-portal-src-5.2.3/portal-web/docroot/html/common/themes/portlet_messages.jspf you will need that file in /WEB-INF/jsp/html/common/themes/portlet_messages.jspf
image
Build and deploy your hook plugin into your Liferay server and you are done!
Popularity: 79% [?]

