Studio Developer Manual / Version 2204
Table Of ContentsWhen working with the metadata tree, you have two data structures to your convenience:
@coremedia/studio-client.main.editor-components/sdk/preview/metadata/MetadataTree
: This data structure represents the whole tree and, for example, offers methods for accessing specific nodes (by their ID) or getting a list of all tree nodes (in breadth-first order).@coremedia/studio-client.main.editor-components/sdk/preview/metadata/MetadataTreeNode
: This data structure represents a single metadata tree node. It offers a range of methods like retrieving the parent or the children of a node, finding specific parent nodes upwards in the hierarchy or specific child nodes downwards in the hierarchy or accessing properties of a metadata tree node.
In the following you will find two examples of how to use the metadata tree. Suppose that the JSP templates on the CAE side have been prepared to include metadata about content. At different points throughout the JSP templates the code might look as follows:
... <cm:metadata var="contentMetadata"> <cm:property name="contentInfo"> <cm:property name="title" value="${self.content.title}"/> <cm:property name="keywords" value="${self.content.keywords}"/> </cm:property> </cm:metadata> <div ${contentMetadata}> ... </div> ...
In a preview document there might be multiple of such content-related metadata chunks attached to different DOM nodes. Suppose you want to gather the titles of all the contents that are included in such metadata chunks. One way to gather these titles in an array is the following:
import MetadataTree from "@coremedia/studio-client.main.editor-components/sdk/preview/metadata/MetadataTree"; import MetadataTreeNode from "@coremedia/studio-client.main.editor-components/sdk/preview/metadata/MetadataTreeNode"; //... const metadataService = null; const metadataTree:MetadataTree = metadataService.getMetadataTree(); const result = []; let nodesToProcess = metadataTree.getRoot() ? [metadataTree.getRoot()] : []; let arrayIndex = 0; while (arrayIndex < nodesToProcess.length) { const currentNode:MetadataTreeNode = nodesToProcess[arrayIndex]; if (currentNode.getProperty("contentInfo")) { const title = currentNode.getProperty("contentInfo").title; result.push(title); } if (currentNode.getChildren()) { nodesToProcess = nodesToProcess.concat(currentNode.getChildren()); } arrayIndex++; }
In this example, the whole metadata tree is traversed in a breadth-first manner. For each
node it has to be checked whether it has the contentInfo
property as there
might be metadata nodes with completely other information.
The code can be simplified considerably if a filtered metadata tree is retrieved:
const metadataService = ... ; const metadataTree = metadataService.getMetadataTree(["contentInfo"]); let result = []; const metadataNodesList = metadataTree.getAsList(); metadataNodesList.forEach((node:MetadataTreeNode) => { result.push(node.getProperty("contentInfo").title); });
In this case, the metadata tree is filtered on retrieval, namely for metadata nodes that
contain the contentInfo
property. Now it is sufficient to get all metadata tree
nodes as an array, walk through it and gather the content titles.