slug
title
Developer
Jahia 7.3
Jahia 8
Legacy
Simulate Automatically synchronize name with title in Java
Question
Is it possible in Java to produce pre-calculated name in jcr:title
like in the UI with mix:tilte
.
Answer
Yes with something like the following code:
import org.jahia.services.content.JCRContentUtils;
String key="My Node Name";
JCRNodeWrapper newNode = currentNode.addNode(JCRContentUtils.findAvailableNodeName(currentNode,JCRContentUtils.escapeLocalNodeName(key)), "jnt:YourNodeType");
newNode.setProperty("jcr:title", key);
escapeLocalNodeName
will replace all illegal chars, findAvailableNodeName
will return a name with an increment if a node with the same name already exists in currentNode.
For example, you can write your own algorithm to replace spaces wit a dash like in the Jahia UI.