Using custom or liferay services in liferay topics (speed patterns)?

How to use the method of personalized services in the pace of liferay in speed files, such as init_custom.vm , portal_normal.vm , etc.

I see that liferay provides many helper class variables, such as $portalUtil for PortalUtil , $getterUtil for GetterUtil , etc. inside the init.vm file.

So, is it possible to get an instance of my user services, for example, an instance of com.my.custom.service.MyCustomLocalServiceImpl or liferay services from UserLocalServiceImpl ?

Here is some psuedo code to give an idea of ​​what I need:

 // this code calls method from MyCustomLocalServiceImpl class to fetch items #set ($listOfItems = $myCustomLocalServiceUtil.getAllItems()) // this code calls method from UserLocalServiceImpl class to fetch users #set ($listOfUsers = $userLocalServiceUtil.getUsers(0, 99)) 

Environment: Liferay 6.1 CE GA1

+3
source share
2 answers

Maybe.

  • The following code shows how to get services:

     // Fetching instance of my custom services #set ($myCustomLocalService = $serviceLocator.findService('myCustomServices-portlet', 'com.my.custom.service.MyCustomLocalService')) // Fetching instance of UserLocalServiceImpl #set ($userLocalService = $serviceLocator.findService('com.liferay.portal.service.UserLocalService')) 
  • Then just call the service methods:

     #set ($listOfItems = $myCustomLocalService.getAllItems()) #set ($listOfUsers = $userLocalService.getUsers(0, 99)) 

For Liferay 6.1 CE GA1: I found this class VelocityVariablesImpl (see insertHelperUtilities , insertVariables ), which actually makes all variables and helper utilities available for speed templates.

+6
source

You can expand the speed context used in the theme with custom variables and services using the following hook plugin. Let's say you need to use your own local service.

  • create a hook plugin with the following definition liferay-hook.xml

     <hook> <portal-properties>portal.properties</portal-properties> </hook> 
  • create portal.properties in main/resources (when using maven) or in docroot/WEB-INF/src (when using sdk plug-ins) place the following configuration there

     servlet.service.events.pre=com.my.custom.action.MyCustomPreAction 
  • create a class com.my.custom.action.MyCustomPreAction in your hook that will extend com.liferay.portal.kernel.events.Action

  • implement the run method

     @Override public void run(final HttpServletRequest request, final HttpServletResponse response) throws ActionException { Map<String, Object> vmVariables = (Map<String, Object>) request.getAttribute(WebKeys.VM_VARIABLES); if (vmVariables == null) { vmVariables = new HashMap<String, Object>(1); } vmVariables.put("myCustomServiceUtil", com.my.custom.service.MyCustomLocalServiceUtil.class); request.setAttribute(WebKeys.VM_VARIABLES, map); } 
  • After deploying your hook, you can use your own service in your theme’s speed template

     // this code calls method from MyCustomLocalServiceImpl class to fetch items #set ($listOfItems = $myCustomServiceUtil.getAllItems()) 
+3
source

All Articles