How to use Model Listeners in Liferay 7 DXP ?
As we know that in liferay 7 or DXP everything is a declared as a component/module so to add a model listener in Liferay 7 we need to define the class as a component.
We will be using the @Component class and to declare the component/module. The service class need to be defined as the ModelListener class. The service class defines the types under which to register this Component as a service.
This will allow us to define the model listener which we want to listen to.
@Component(immediate = true, service = ModelListener.class)
We will be extending the BaseModelListener class and overriding the required methods.
Below is the code that can be used to implement the model listener for User model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liferarevisited.model.listener;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
@Component(immediate = true, service = ModelListener.class)
public class CustomUserListener extends BaseModelListener<User> {
@Override
public void onAfterCreate(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onAfterCreate()");
super.onAfterCreate(model);
}
@Override
public void onAfterRemove(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onAfterRemove()");
super.onAfterRemove(model);
}
@Override
public void onAfterUpdate(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onAfterUpdate()");
super.onAfterUpdate(model);
}
@Override
public void onBeforeCreate(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onBeforeCreate()");
super.onBeforeCreate(model);
}
@Override
public void onBeforeRemove(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onBeforeRemove()");
super.onBeforeRemove(model);
}
@Override
public void onBeforeUpdate(User model) throws ModelListenerException {
System.out.println("CustomUserListener.onBeforeUpdate()");
super.onBeforeUpdate(model);
}
}
If we create a new user using the create account page or from the control panel we would see that the desired model is getting called.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.liferarevisited.model.listener; | |
import org.osgi.service.component.annotations.Component; | |
import com.liferay.portal.kernel.exception.ModelListenerException; | |
import com.liferay.portal.kernel.model.BaseModelListener; | |
import com.liferay.portal.kernel.model.ModelListener; | |
import com.liferay.portal.kernel.model.User; | |
@Component(immediate = true, service = ModelListener.class) | |
public class CustomUserListener extends BaseModelListener<User> { | |
@Override | |
public void onAfterCreate(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onAfterCreate()"); | |
super.onAfterCreate(model); | |
} | |
@Override | |
public void onAfterRemove(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onAfterRemove()"); | |
super.onAfterRemove(model); | |
} | |
@Override | |
public void onAfterUpdate(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onAfterUpdate()"); | |
super.onAfterUpdate(model); | |
} | |
@Override | |
public void onBeforeCreate(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onBeforeCreate()"); | |
super.onBeforeCreate(model); | |
} | |
@Override | |
public void onBeforeRemove(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onBeforeRemove()"); | |
super.onBeforeRemove(model); | |
} | |
@Override | |
public void onBeforeUpdate(User model) throws ModelListenerException { | |
System.out.println("CustomUserListener.onBeforeUpdate()"); | |
super.onBeforeUpdate(model); | |
} | |
} |
We can see that while a user is getting created all the model listener methods that we had overridden are getting called.
RELATED POSTS
RELATED POSTS
Thanks for your all posts. It helps me a lot.
ReplyDeleteFor modal listener, Do I need to create module project from IDE or Blade CLI ?
You can use any. Best you should create an API template module and create this class in that API module.
DeleteThanks a million.You made my day.
ReplyDelete