# Recursively builds a tag tree, starting from the specified path
def tagTree(path):
tagTreeList = []
newItem = {}
# Browse the curent path.
results = system.tag.browse(path)
# Add each tag that was found to a new item.
for result in results.getResults():
newItem['label'] = result['name']
newItem['data'] = result['fullPath']
# If the tag has children, browse and add each of the children to the item.
if result['hasChildren']:
newItem['items'] = tagTree(result['fullPath'])
# Once all children have been added to the item, add the new item to the list.
tagTreeList.append(newItem.copy())
# After all items under the path have been added to the list, return the list.
return tagTreeList
I couldn’t find this answered anywhere on the forum, here’s what I came up with.
First, thanks so much for this Ben, this is exactly what I needed.
One note – newItem should be initialized under the for result in results.getResults(): loop. Otherwise, if the next tag in the list does not have any children, it just inherits the ‘items’ from the previous tag.