How to make many-to-many relationships between assets


(Julian) #1

Matrix Version: 5.4.1.2

Our website has dozens of authors uploading content. There are occasions when they produce multiple articles that are the same.

For reporting purposes I need to create a tool that is able to show me two articles that are exactly the same.

My idea is to create a metadata field that allows content authors to connect two or more articles. And then create a asset listing that lists articles which have a connection.

I have experimented with Related asset metadata field, however this is not quite right because the connection this metadata field creates is 1 way only. It will allow authors to select a related asset in the related asset metadata field, but if you view the metadata of the related asset the original asset is not shown.

I need a related asset metadata field such that If I am viewing article A and select article B as a related asset, and then navigate to article B, article A will appear as a related asset as a result of selecting article B as a related asset while viewing asset A. Effectively creating a two way connection by performing one action.

An example of such a tool created for a different system is here: https://wordpress.org/plugins/posts-to-posts/

How do i go about creating this type of many-to-many connection between assets?


(Tim Davison) #2

I second this question - would be very useful to have.

Prior to v5.4 we sometimes just used NOTICE links (we used a lot of javascript to alter the DOM to look like we needed), but since v5.4 we’ve been using container templates, which are great, but now there’s no longer any NOTICE links (which is not great). 2-way association, including both manual and/or automatic like NOTICE links, would be brilliant.


(Peter McLeod) #3

Hi
if you were wanting to continue with your initial solution (using metadata related assets and an asset listing), you could use server side js to do what you need:

Something like:

In the default format, an array of ids, the 1st item is the listed asset id and the 2nd is an array of ids that are set in the Related metadata field :

%asset_position^gt:1:,:%[%asset_assetid%,%asset_metadata_Related^empty:[]%]

In the page contents:

<script runat="server">

  var listing = [%asset_listing%];
  var relations = {}; 

  listing.forEach(function(item) {
        var asset = item[0]; //asset id
        var relatedAssets = item[1]; //ids of related assets
        
        if(typeof relations[asset] === "undefined"){
            relations[asset] = relatedAssets; //set the initial value for the asset, either the array of related assets or empty array
        } 

        relatedAssets.forEach(function(relatedAssetId){ //iterate through each related id

            if(typeof relations[relatedAssetId] === "undefined"){ 
                relations[relatedAssetId] = [asset]; //add its id as a key in the relations object if it isn't there
            } else {
                relations[relatedAssetId] = relations[relatedAssetId].concat([asset]); // if it already exists, add the current item asset id to its value
            }
            
        });
 
  });
  
  print("Creates an object with keys for each asset and value of an array of ids of assets that are either set in this asset's metadata as a related asset, or list this asset in the metadata as a relation:");
  print(JSON.stringify(relations));
  
  // could then use further js to output as needed, eg with global keyword replacements to get name, links etc
  
 </script> 

One thing to note is that the scope of the asset listing would need to include all assets that could be related. This may be inefficient depending on how its implemented.

Could potentially also use a trigger set a field on the other asset when the related field is set.

Thanks
Peter


(Bart Banda) #4

One way I think I’ve done it in the past is to have a stored search page embedded on the page that does a match on all assets that have the current asset’s ID in their related metadata field value.

For example,
Asset A - #1111
Asset B - #2222

Asset A > Set related field value to #2222
On Asset B, do a search and find all assets that have #2222 in their metadata related field.


(Julian) #5

Thanks for the responses everyone. This is over my skill level so I will submit a support request. thanks