Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a function on a Liferay native portlet

I am making a change to a native Liferay portlet, that comes with Liferay Intallation. How can I change a function to my own implementation by a hook or similar approach?

I have read how to make pre and post conditions and how to make new implementation of an interface, but I don't know how to just replace random function inside random class at a portlet that I want to keep otherwise as it is originally.

like image 237
mico Avatar asked Jul 29 '11 11:07

mico


1 Answers

There are several ways you can modify the functionality of a native Liferay portlet. Here is a quick overview.

Most hooked functionality is described through the liferay-hook.xml file located in your /docroot/WEB-INF directory. Here are the most common method.

Custom JSPs

In the liferay-hook.xml file, add the following child to <hook/>

<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>

This element defines the location of where you are going to place JSPs to be overwritten. For example you may want to rewrite view.jsp for the Document Library portlet at:

[custom-jsp-dir]/html/portlet/document_library/view.jsp

Model Listeners

For this one you'll have to define a portal.property file typically stored at,

/docroot/WEB-INF/src/portal.property

And tell liferay-hook.xml its location. The following is an example for the above,

<portal-properties>portal.properties</portal-properties>

if you want to listen to changes in User, for example, you would write in the property,

value.object.listener.com.liferay.portal.model.User=com.my.example.UserListener;

Which is in the following format,

value.object.listener.[class-to-listen]=[my-listener-class]

And your class should implement com.liferay.portal.model.BaseModelListener.

Here you can listen to events such as Add, Update, Remove, and a few others.

Extend\Overwrite Service

A similar story here, in liferay-hook.xml in the <hook /> element add

<service>
    <service-type>com.liferay.portal.service.UserService</service-type>
    <service-impl>my.example.service.UserServiceImpl</service-impl>
</service>

Here your implementation should extend the correct wrapper class for a particular service. For the example above, for instance, is

com.liferay.portal.service.UserServiceWrapper;

You should now be able to overwrite all the public methods for the UserService like updateUser(..).

Customization of Struts Actions

(Only available from Liferay 6.1 version)

In very similar fashion as extending services, define the elements for <hook />

<struts-action>
    <struts-action-path>/message_boards/view</struts-action-path>
    <struts-action-impl>my.example.action.SampleViewAction</struts-action-impl>
</struts-action>

You'll need to extend,

com.liferay.portal.kernel.struts.BaseStrutsAction

and you'll have access to the request and can perform a custom action. Which is very powerful in conjunction with custom JSPs.

Good Luck!

Be sure to check compatibility with the version of Liferay you are using.

If you need even more control you would need to use the ext-plugin.

like image 82
rp. Avatar answered Sep 21 '22 01:09

rp.