Javascript API - discovering the thumbnail asset id for an asset


(J Stephen) #1

Is it possible to discover the asset id of an asset's thumbnail using the Javascript API? I thought it might be an attribute value or a general info setting, but I don't see it using either getAttributes or getGeneral.


Neither does it appear in getChildren on the asset or getParents on the thumbnail, although you do see the thumbnail as a NOTICE link on the linking page in Matrix for the asset.



Does anyone know the best way to discover an asset's thumbnail using the Javascript API?


(Anthony Barnes) #2

One possible way off the top of my head is to use the simple edit keywords to extract the id information. I don't think it's directly available through the JS API itself, but you could do something like:

    
    var assetid = 1234;
    getKeywordsReplacements(assetid,["%details-S_thumbnail%"],function(data){
    var html = data["details-S_thumbnail"]["details-S_thumbnail"];
    var idRegExp = new RegExp('Id: #([0-9]+)','gi');
    var match = idRegExp.exec(html);
    if (match !== null) {
       alert('The asset id of the thumbnail is " + match[1]);
    }
    });


I could be wrong, but I think that relies on the user invoking the api call having write access to the asset. You could also analyse the linking screen keywords to extract the same information, but I think it's a little easier this way.

(Scott Hall) #3

Yep antoine's solution should be a goer, another option is to cache the returned data as a jQuery object and then traverse the data and pluck out what you require.

    
    var assetid = 1234;
    getKeywordsReplacements(assetid,["%details-S_thumbnail%"],function(data){
    var $detailsHTML = jQuery('
'+data["details-S_thumbnail"]["details-S_thumbnail"]+'
'); var thumbnailId = $detailsHTML.find('input[name$="thumbnail_assetid"]').val(); });


I havnt tested that, and you'd want some conditions in there to handle null values etc, but gets the juices flowing.

(Scott Hall) #4

Woops my input selector will have you chasing you're tail there, try:

    
    var assetid = 1234;
    getKeywordsReplacements(assetid,["%details-S_thumbnail%"],function(data){
    var $detailsHTML = jQuery('
'+data["details-S_thumbnail"]["details-S_thumbnail"]+'
'); var thumbnailId = $detailsHTML.find('input[id$="thumbnail[assetid]"]').val(); // console.log(thumbnailId); // Do stuff with thumbnail input value here. });

(J Stephen) #5

getKeywordsReplacements works very well. Thank you very much.


It's also a very handy function which I hadn't known about before, so thanks for pointing it out.