'trying to access my custom util in freemarker - Liferay

I have a utility to access my custom Liferay configuration (not default Liferay), and I am able to do what I want in java code, but when it comes to freemarker I got stuck.

Here is my class that I want to access in Liferay theme freemarker Template:

class name : CommonServicesUtil


package mycompany.liferay.modules.common_services.util;

public class CommonServicesUtil {

    public static CommonServicesGroupConfiguration getCommonServicesGroupConfiguration(final long groupId) {
        try {
            return ConfigurationProviderUtil.getConfiguration(CommonServicesGroupConfiguration.class, new GroupServiceSettingsLocator(groupId, CommonServicesConstants.COMMON_SERVICES_API_BUNDLE_NAME));
        } catch (ConfigurationException ex) {
            throw new ArenaRuntimeException(ex.getMessage(), ex);
        }
    }

    public static CommonServicesSystemConfiguration getCommonServicesSystemConfiguration() {
        try {
            return ConfigurationProviderUtil.getSystemConfiguration(CommonServicesSystemConfiguration.class);
        } catch (ConfigurationException ex) {
            throw new ArenaRuntimeException(ex.getMessage(), ex);
        }
    }
}

in freemarker I tried the follwoing :

\<#assign configuration = serviceLocator.findService("mycompany.liferay.modules.common_services.util.CommonServicesUtil")\>

\<#assign ConfigurationProvider = objectUtil("mycompany.liferay.modules.common_services.util.CommonServicesUtil")  /\>

\<#assign ConfigurationProvider = staticUtil\["mycompany.liferay.modules.common_services.util.CommonServicesUtil"\]  /\>

My liferay version is 7.4

I am trying to access the configuration that I have been added to Liferay configuration



Solution 1:[1]

I've always assumed that serviceLocator was for Liferay's Service Builder's classes. If you need arbitrary classes, I wouldn't know how it would find those classes on the classpath (it'll be from potentially many OSGi bundles) At a minimum, you'd need to export the class or interface.

I'd rather recommend to implement a TemplateContextContributor, which makes the template so much cleaner, and don't require you to mock around with identifying specific implementation objects.

Solution 2:[2]

There is a good way to get what you want by using Template-Context-Contributor Please have a look on liferay website

there is a sample for that here

Create a new module by running blade command for Template-Context-Contributor or use maven check liferay website , then you will end up with a new module has the following class :

 package com.liferay.blade.samples.theme.contributor;

 import com.liferay.portal.kernel.template.TemplateContextContributor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;

@Component(
  immediate = true,
  property = "type=" + TemplateContextContributor.TYPE_GLOBAL,
 service = TemplateContextContributor.class
)
public class BladeTemplateContextContributor
  implements TemplateContextContributor {

/**
 * Injects a new string variable into the map of provided variables. The map
 * is made available to non-JSP templates (FreeMarker, Velocity, etc.) that
 * do not have access to the contextual objects native to the platform, like
 * the request and session.
 *
 * <p>
 * The <code>sample_text</code> variable can be used in any theme file.
 * For example, you could add it to the <code>portal_normal.ftl</code> file
 * in your theme as <code>${sample_text}</code>.
 * </p>
 *
 * @param contextObjects the variables available in the context
 * @param httpServletRequest the HTTP servlet request
 */
@Override
public void prepare(
    Map<String, Object> contextObjects,
    HttpServletRequest httpServletRequest) {

    contextObjects.put("sample_text", "This is some sample text");
    }
 }

after you package and deploy to liferay tomcat you will be able to access the values from freemarker, velocity , theme

In freemarker like this : ${sample_text}

In addition you can see the scope here is GLOBAL property = "type=" + TemplateContextContributor.TYPE_GLOBAL

you can change it to theme if you would like to get the values in the themes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olaf Kock
Solution 2