Laliwala IT Services

Laliwala IT Services
Website Development

Friday, January 17, 2020

ModelListener Hook in Liferay DXP 7.1




Model Listeners are used for listening to the Liferay Model Events.Model Listeners implement the ModelListener interface. They are used to listen for persistence events on models and perform actions based on requirement either before or after creation of event.

Model listeners were designed to perform lightweight actions in response to a create, remove, or update attempt on an entity’s database table or a mapping table.

For example, if one wants to perform a specific action after deleting a user, this can be implemented using a model listener.

Requirements:Liferay 7 Dxp and knowledge of creating liferay hooks

You can create a model listener in a module by doing two simple things:
     Implement ModelListener
     Register the service in Liferay’s OSGi runtime

Step 1: Create a model Listener class that extends BaseModelListener to override listener methods from parent class. Here in CustomEntity you can define any entity of liferay as well as custom entity also. Here I have used User as an entity.




Step 2: Register the Model Listener Service
Register the service with Liferay’s OSGi runtime. If using Declarative Services, set service= ModelListener.class and immediate=true in the Component.

Step 3: Now implement listener methods and write your custom logic to perform actions. Here I have used onAfterRemove() and onBeforeRemove().

There are multiple methods of model listener used to perform action on model.
Listening for Persistence Events:
onAfterAddAssociation
onBeforeAddAssociation
onAfterCreate
onBeforeCreate
onAfterUpdate
onBeforeUpdate
onAfterRemove
onBeforeRemove
onAfterRemoveAssociation
onBeforeRemoveAssociation



 People also like : 


ADVANTAGES OF LIFERAY 7/DXP DEVELOPMENT TRAINING

and 


Learn Use of Hook in Liferay with examples


 https://www.youtube.com/watch?v=oX9qsUjPsGU

.



No comments:

Post a Comment