Skip to main content

How do I access a chooser field's chosen asset?

With the Cascade API

Choosers expose an asset property, set to the chosen asset's API object if one is chosen, or null if not. Because it can be null, check before accessing its properties:

Velocity
#set ($thumbnailImage = $currentPage.getStructuredDataNode("thumbnail-image"))
#if (!$_PropertyTool.isNull($thumbnailImage.asset))
    <img src="${thumbnailImage.asset.link}"/>
#end

With an Index Block and the XPath Tool

In an Index Block, a chooser field contains XML elements describing the asset — name, path, link, site, and so on. If no asset is chosen, you get a path of / and no other elements. Check first here too:

Velocity
#set ($thumbnailImage = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/calling-page/system-page/system-data-structure/thumbnail-image[path != '/']"))
#if ($thumbnailImage.size() > 0)
    <img src="${thumbnailImage.getChild("link").value}" />
#end

Not seeing the chosen asset's contents?

By default, the structured data content for a chosen asset isn't present in an Index Block rendering, or in the generated system-data-structure in the DEFAULT region when no Index Block is assigned. You'll see an empty <content/> element instead.

To include it, edit the Data Definition containing the chooser field, open the chooser field's settings, and set Render Content Depth to at least 2. Depending on the structure of the chosen asset, a higher value may be needed.