Message Boards

Liferay 7.2 - Can I inject my code on Liferay existing classes?

Fabio Carvalho, modified 4 Years ago.

Liferay 7.2 - Can I inject my code on Liferay existing classes?

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
I want to add some data to my database when the user performs some actions. If a user deletes a page for example, I want to add a row with some information on my database. To do this I would like to add one or two lines of my code into the class/method that deletes the page to add that data to my database also.

I have already created the database and tried to add some rows in my table for tests purposes and is working. Now I would like to move this code to the class that process the deletion of the pages for example.

It is possible to inject code of mine in the classes that already exists? What would be the best approach to do this?
Matthew K., modified 4 Years ago.

RE: Liferay 7.2 - Can I inject my code on Liferay existing classes?

Junior Member Posts: 74 Join Date: 8/31/15 Recent Posts
You should consider using model listeners for this purpose. Then you don't have to override already existing code.
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Liferay 7.2 - Can I inject my code on Liferay existing classes?

Liferay Legend Posts: 14914 Join Date: 9/2/06 Recent Posts
Matthew is right here, the model listener is the way to go. It is, in fact, the approach Liferay recommends for adding Audit tracking for adds, updates and deletes.