Simulate Automatically synchronize name with title in Java

  Written by The Jahia Team
 
Developers
   Estimated reading time:
7.1.1.0

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.

Related links