如何以编程方式创建Liferay 7 Web内容模板?

时间:2017-06-06 07:34:46

标签: liferay freemarker liferay-7

在Liferay 7中,我有一个结构,一些FTL代码,我想在Java Liferay模块(没有portlet)中创建一个模板。就像人类会使用下面的UI一样,但是以编程方式:

Liferay 7 New Template

注意:How to create Structure & Template programmatically in Liferay 6提议的代码不起作用,导致Someone may be trying to circumvent the permission checker例外。

另请参阅:How to create a Liferay 7 structure programmatically?

1 个答案:

答案 0 :(得分:1)

此代码采用structureDDMStructure)并成功创建模板:

ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(group.getGroupId());
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);

String templateKey = "my template"; // Any name you want.

long classNameId = PortalUtil.getPortal().getClassNameId(DDMStructure.class);
long classPK = structure.getPrimaryKey();
long resourceClassNameId = PortalUtil.getPortal().getClassNameId(JournalArticle.class);
nameMap = Utils.createLocalesMap(templateKey);
descriptionMap = nameMap; // Use the same.
String type = "display";
String mode = "create";
String language = "ftl";
String script = null;
try {
    script = new String(Files.readAllBytes(Paths.get("/path/to/my/template.ftl")));
} catch (IOException e) {
    log.error("Exception when reading template: " + templateDefinitionFilePath, e);
}

try {
    DDMTemplate template = DDMTemplateLocalServiceUtil.addTemplate(
        user.getUserId(), group.getGroupId(), classNameId, classPK,
        resourceClassNameId, nameMap, descriptionMap,
        type, mode, language, script, serviceContext);
} catch (PortalException e) {
    log.error("Exception when creating template: " + templateDefinitionFilePath, e);
    return false;
}