damfn

damfn templating functions provides access to digital assets such as images. Use these functions to get an asset from Magnolia’s built-in DAM or other asset providers. DAM functions also create asset renditions and links to assets.

Get asset

Retrieves an Asset by itemKey.

Method signature

AssetgetAsset(String``itemKey)

Arguments

Argument Description

itemKey

required

A key that uniquely identifies an asset. The key is composed of an asset provider ID and a provider-specific asset ID.

Returns

Asset

An asset by the given itemKey.

Usage

damfn.getAsset(itemKey)

[#assign itemKey = cmsfn.asContentMap(componentNode).image]
[#assign myAsset = damfn.getAsset(itemKey)]

In this example componentNode is the node of a Text and Image component.

  • AssetgetAsset(String providerId, String assetPath). Returns an asset from a given asset provider. The asset provider does not have to be Magnolia JCR.

  • AssetgetAssetForAbsolutePath(String providerId, String absoluteAssetPath)

Get folder

Gets a Folder based on an itemKey. Deleted or hidden folders are not returned.

Method signature

FoldergetFolder(String``itemKey)

Arguments

Argument Description

itemKey

required

A key that uniquely identifies an asset. The key is composed of an asset provider ID and a provider-specific asset ID.

Returns

Folder.

A folder by the given item key.

Usage

damfn.getFolder(itemKey)

[#assign contentPageNode = cmsfn.root(currentNode).getNode("home")]
[#assign componentNode = contentPageNode.getNode("main/0") ]
[#assign folderItemKey = cmsfn.asContentMap(componentNode).image]
[#assign myAsset = damfn.getFolder(folderItemKey)]
folder-name: ${myAsset.name}
  • FoldergetFolder(String providerId, String folderPath)

Get asset rendition

Gets the AssetRendition for a given itemKey and renditionName. If the Asset exists but the rendition is not defined, it returns default rendition which is the same as the original asset.

Method signature

AssetRenditiongetRendition(String``itemKey, String renditionName)

Arguments

Argument Description

itemKey

required

Composite key that identifies the asset’s provider ID and provider-specific ID.

renditionName

required

Name of the rendition.

Returns

AssetRendition

Usage

damfn.getRendition(itemKey, renditionName)

[#assign itemKey = cmsfn.asContentMap(componentNode).image]
[#assign myRendition = damfn.getRendition(itemKey, "small-square")!]

The example above assumes that you have defined a rendition (image variation) named small-square in your Theme.

Create asset property map

Creates a map for a given Asset or a given itemKey containing all the bean properties of the asset. Additionally the map may contain further nested maps with the properties of classes implementing info.magnolia.dam.api.metadata.AssetMetadata. The metadata submaps are collected under the key metadata.

Method signature

Map<String, Object> getAssetMap(``Assetasset)

Map<String, Object> getAssetMap(String``itemKey)

Map<String, Object> getAssetMapForAssetId(String``itemKey) *

*) getAssetMapForAssetId(String itemKey) and getAssetMap(String itemKey) are both identical except for handling of non-existent assets. While the getAssetMapForAssetId is null-safe, the getAssetMap is not.

You should always check for existence of assets or provide a fallback when using the getAssetMap method.

Arguments

Argument Description

asset

required

Asset

itemKey

required

The composed item key.

Returns

Map<String, Object>

A map with the properties of an asset.

Usage

damfn.getAssetMap(asset) or damfn.getAssetMap(itemKey)

[#assign myAsset = damfn.getAsset("jcr", "photos/pool.jpg")!]
[#assign myAssetMap = damfn.getAssetMap(myAsset)]
[#-- assign myAssetMap = damfn.getAssetMap("jcr:20d6e4e3-fe53-4b23-8d64-6e67a1c1667f")--]
fileSize: ${myAssetMap.fileSize}
name: ${myAssetMap.name}
title: ${myAssetMap.title}
width: ${myAssetMap.metadata.mgnl.width}
format: ${myAssetMap.metadata.dc.format}
  • Line 3 shows an alternative for line 1-2.

  • The existance of the metadata submaps (see lines 7, 8) depends on the implementation of your info.magnolia.dam.api.AssetProvider (see Map of an asset and its metadata). When JcrAssetProvider (fetching assets for the workspace dam) metadata.mgnl and metadata.dc are available.

Gets a link to an Asset. Returns null if Asset is not found or in case of an exception.

Method signature

String getAssetLink(String``itemKey)

Arguments

Argument Description

itemKey

required

Composite key that identifies the asset’s provider ID and provider-specific ID.

Returns

String

The link to an asset by a given asset key.

Usage

damfn.getAssetLink(itemKey)

 <img src="${damfn.getAssetLink("jcr:324aa329-7acc-4a21-a05e-d5f9b69bf7e4")}"/>

Gets the link to the AssetRendition for a given itemKey and renditionName. Returns null in the case of an exception or if Asset or AssetRendition are not found. If the Asset exists but the rendition is not defined, it returns the link to the default rendition which is the same as the original asset.

Method signature

String getAssetLink(String``itemKey, String renditionName)

Arguments

Argument Description

itemKey

required

Composite key that identifies the asset’s provider ID and provider-specific ID.

renditionName

required

Name of the rendition.

Returns

String

The link to an asset rendition by given item key and rendition name.

Usage

damfn.getAssetLink(itemKey, renditionName)

 <img src="${damfn.getAssetLink("jcr:324aa329-7acc-4a21-a05e-d5f9b69bf7e4", "small-square")}"/>
  • String getAssetLink(Asset asset, String renditionName)

Related topics
Feedback

DX Core

×

Location

This widget lets you know where you are on the docs site.

You are currently perusing through the DX Core docs.

Main doc sections

DX Core Headless PaaS Legacy Cloud Incubator modules