Groovy Shell
for Magnolia
Leverage the power of Groovy inside Magnolia
Usage & Samples
Sets a property in a list of pages
The following shows how you can set a property for all the pages using a specific template from the Groovy Shell.
// a simple script that sets a property for all the pages that match
// a given template in a list of paths
import info.magnolia.context.MgnlContext
import info.magnolia.cms.util.NodeDataUtil
hm = MgnlContext.getSystemContext().getHierarchyManager("website")
count = 0
str = ""
basePaths = ["/path1", "/path2"]
//for each node in given paths
goDeep = { node ->
/*
only if the page match a given template.
Note: in groovy get methods with no arguments can be called
by omitting the prefix 'get' and the round brackets
e.g. node.getMetaData() -> node.metaData
*/
if ("sometemplate".equals(node.metaData.template))
{
// change the nodeData "propertytochange" to "new"
NodeDataUtil.getOrCreateAndSet(node, "propertytochange", "new")
str += "\nSetting property for " + node.handle
}
cs = node.getChildren(info.magnolia.cms.core.ItemType.CONTENT)
cs.each{
goDeep(it)
}
}
basePaths.each{
node = hm.getContent(it)
goDeep(node)
}
hm.save()
return str + "\n" + count + " pages changed"
