How do I access a chooser field's chosen asset?
Cascade API
When working with the Cascade API and choosers, there is an asset
property which will be set to the chosen asset's API Object if an asset is chosen, or null
if no asset is chosen.
Because the asset
property will be null
if no asset is chosen , it is advised to ensure an asset is chosen before attempting to access it's properties.
Consider the following example:
#set ($thumbnailImage = $currentPage.getStructuredDataNode("thumbnail-image"))
#if (!$_PropertyTool.isNull($thumbnailImage.asset))
<img src="${thumbnailImage.asset.link}" />
#end
Index Block and XPath Tool
When working with Index Blocks and choosers, the chooser field will contain a number of XML elements which provide information about the given asset, such as: name, path, link, site, etc. If no asset is chosen, a path
of /
will be present and no additional XML elements.
Much like accessing chosen assets using the Cascade API, it is advised to first check if an asset is chosen before attempting to access more information about the chosen asset.
Consider the following example:
#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 contents of the chosen asset?
By default, the structured data content for a chosen asset will not be present within an Index Block rendering, or through the generated system-data-structure
within the DEFAULT region if no Index Block is assigned. Instead you will see an empty <content/>
element.
In order to include this content, edit the Data Definition containing the chooser field, open the chooser field's settings and set the Render Content Depth field to a value of at least 2
.
Note: Depending on the structure of the chosen asset, a higher value may be required.