Matrix Version: 5.5.3.3
Hey all,
I’m trying to display an image based on a select key/value in a metadata schema set up like this:
Key value
1000 apple
1001 banana
1002 grape
pear pear
The user can select multiple options and the key in this case is the asset id of the image to display.
The issue is that not all values necessarily have an image so in the example above ‘pear’ has no image so the key is just ‘pear’ as a key is required but I obviously don’t want to send this in the final array.
I’ve run through some tests and have a working result (icons8 below gives me what I need) but it just seems a bit fragile and on the hacky side and I’m worried it might break depending on the value entered for the key. eg if the user leaves a space at the end of the key.
Is there a better solution for this that I haven’t thought of?
Cheers
My server side JS looks like:
<script runat="server">
const icons1 = %asset_metadata_activities.allowed_key%; //returns the base string with each key
const icons2 = %asset_metadata_activities.allowed_key^explode:;%; //creates an array from the string
const icons3 = %asset_metadata_activities.allowed_key^explode:;^as_asset:asset_assetid%; //fails due to spaces in array values
const icons4 = %asset_metadata_activities.allowed_key^explode:;^trim%; //trim does nothing
const icons5 = %asset_metadata_activities.allowed_key^explode:;^replace:\s:%; //this removes all spaces
const icons6 = %asset_metadata_activities.allowed_key^explode:;^replace:\s:^as_asset:asset_assetid%; //but fails to return anything here and also breaks the script
const icons7 = %asset_metadata_activities.allowed_key^explode:; %; //adding a space into the explode delimiter works to remove spaces
const icons8 = %asset_metadata_activities.allowed_key^explode:; ^as_asset:asset_assetid%; //and then ^as_asset returns an array exactly as needed
const icons = %asset_metadata_activities.allowed_key^explode:; ^as_asset:asset_assetid%;
if(icons.length > 0){
print('<ul class="parks__icon-list">');
icons.forEach(function(icon){
print('<li><img src="./?a=' + icon + '"></li>');
});
print('</ul>');
}
</script>
And the results are:
const icons1 = 1737589; 1737594; 1737588; 1737591; 230; //returns the base string with each key
const icons2 = ["1737589"," 1737594"," 1737588"," 1737591"," 230"]; //creates an array from the string
const icons3 = ["1737589"]; //fails due to spaces in array values
const icons4 = ["1737589"," 1737594"," 1737588"," 1737591"," 230"]; //trim does nothing
const icons5 = ["1737589","1737594","1737588","1737591","230"]; //this removes all spaces
const icons6 = ; //but fails to return anything here and also breaks the script
const icons7 = ["1737589","1737594","1737588","1737591","230"]; //adding a space into the explode delimiter works to remove spaces
const icons8 = ["1737589","1737594","1737588","1737591"]; //and then ^as_asset returns an array exactly as needed