Accessing values in a related asset metadata field


#1

Matrix Version: 5.3.4.0

Hi, does anyone know how to access certain values in a related asset metadata field?

According to this page - https://matrix.squiz.net/manuals/metadata-schemas/chapters/metadata-fields#related-asset-field you can increase the number of max selections and when you do they are all stored in an array but then how do for instance access the 3rd value using keywords?

e.g. %globals_asset_metadata_test???%

Edit: Is an array for a related asset metadata field not a proper array?

e.g.

In my design parse file if I do this -

alert("%globals_asset_metadata_test^index:0%");

This will not work however I can do this -

var test = %globals_asset_metadata_test%;
alert(test[0]);

And it works?? But the problem with the second one is I can’t use that asset id number and pass it back into Matrix keywords for me to use or can I?


(gja) #2

I have a similar problem but am on version 5.4.1.2

I have a multi related metadata field, and am wanting to get the name of the asset from the id.

To add additional complication, I’m in a nested asset in a paint layout and the metadata field is on the parent asset.
%globals_asset_parent^as_asset:asset_metadata_related^empty:[]%; returns 58161,58606,59858

But alas print(’%’+assetID+’^as_asset:asset_name^lowercase%
’); doesn’t work.

What I want to do is look for an asset in the related metadata field whose name matches a metadata value in the current asset.
i.e.
if (’%’+assetID+’^as_asset:asset_name^lowercase%’ == ‘%globals_asset_metadata_name^lowercase%’){
do if matches
}

Any ideas?


(Bart Banda) #3

Metadata values when stored and retrieved are all stored as text strings, not array objects. So if you want to use array based keyword modifiers on array strings, you first need to convert it to an array using ^json_decode modifier.
So try this:

alert("%globals_asset_metadata_test^json_decode^index:0%");

This is tricky to do without using an asset listing (since you are not on 5.4.2.2). The best way would probably be to pass that array of asset IDs to an asset listing and do your comparison in the Type Format?

The other way, which I’m not sure if it will work, is to do something like this:

var assets = %globals_asset_parent^as_asset:asset_metadata_related^empty:[]%;
assets.forEach(function(assetID){
  print('%' + 'globals_asset_name:' + assetID + '^eq:{globals_asset_name}:TRUE:FALSE%');
});

?


(gja) #4

Hi,

I’ve tried

print('%' + 'globals_asset_name:' + assetID + '^eq:{globals_asset_name}:TRUE:FALSE%');
works as expected printing true or false as required.

However,

 if('%' + 'globals_asset_name:' + assetID + '^eq:{globals_asset_name}:TRUE:FALSE%'){
                print('found matching at'+ assetID );
 }

Doesn’t work presumably because the evaluation of the keyword replacement is occurring after the SSJS has executed.

I’ll try the asset listing option.


(Bart Banda) #5

Yes, that’s correct, but using the first method you should be able to print whatever you want.

print('%' + 'globals_asset_name:' + assetID + '^eq:{globals_asset_name}:'+
  //the true content
  '<div>Some content here and possibly more keywords</div>'+
  ':'+
  //the false content
  '<div>Some alternative content>'+
  //close keyword
  '%');

(Charlotte Westney) #6

I’m trying to search against a metadata field with an array of related asset IDs. Can not get it to work. If I pass the form a set value against a simple text metadata field, I get search results. But if I pass it a set value of an asset ID against the related metadata field (and have a page with that ID selected in the related metadata field) I get no results.

Is that because the array is stored as text, and so each ID isn’t seen as an individual word to match on?
How do I get my search to correctly match against a related metadata array?

Thanks!
Charlie.


(Bart Banda) #7

Whats the stored value of your metadata related field? Is it something like “[1111,2222,3333]”?

I have a feeling that Matrix probably might treat that as a single word rather than separate ones?

One thing you could test is to use another metadata field that just sources the value of your related metadata field as a default value, but also does a keyword modifier on it, something like:

%metadata_field_related_assets^json_decode^as_csv^replace:,: %

Or something like that, so that you effectively get a value of “1111 2222 3333” instead.