Get metadata key and value in an array


(gja) #1

Matrix Version5.4.5.1:
Hi,

I’m wanting to return the selected key and value in an array.
For example [1:entre,2:main,3:pudding], where 1,2,3 are the key for the select field and entre, main and pudding.

I know that there is %asset_metadata_name_key% and %asset_metadata_name_value% which will return selected values in separate lists.
%asset_metadata_name% - only returns the part that is selected to show on frontend.

Any ideas?

Thanks Georgina.


(Luke Wright) #2

Hi Georgina,

So am I right in that you’re asking about possibly a multiple-selection Select field, and you want the values you select returned together as, say, a JSON array?

At the moment I don’t believe there is a feature that provides what you want for that, no.

If I haven’t guessed right, please clarify.


(gja) #3

Hi Luke,

Yes I think you have got it - I want both key and value togoether.

The use case is that I need both parts.

I will find another solution.

It seems weird that we specify the key and value when setting up the metadata schema but can only get the information returned via keywords separately.

Georgina.


(Bart Banda) #4

Hey @georgina, you should actually be able to get this via the %asset_data_attributes% keyword. For example,

%globals_asset_data_attributes:1234^index:select_options^index:value%

Where 1234 is the asset ID of the Metadata Select Field, will give you something like:

{a: “1”, b: “2”, c: “3”}

Available from Matrix version 5.4.2.0.


(gja) #5

That keyword gives me all the available options not just the key/value pairs that have been selected.

I ended up using ssjs

<script runat="server">
var value = %asset_metadata_field_value^explode:;^empty:[]%;
var url = %asset_metadata_field_key^explode:;^empty:[]%;
var number = %asset_metadata_field^explode:;^count%;
print('<ul>');
for (var i = 0, len = number; i < len; i++) {
      print('<li><a href=url[i].trim()+'">'+value[i]+'</a></li>'); 
}
print('</ul>');
</script>

This seems to do what I want and is quite neat.

Georgina.