Firing a trigger on JS API updateFileAssetContent events


(Phil Mcphee) #1

Hi there

 

I'm building an image file uploader using the JS API. It goes a bit like this:

js_api.createFileAsset({
	"parentID":newResourceId,
	"friendly_name":resourceTitle.toLowerCase().replace(' ', '_') + extension,
	"type_code":'image',
	"link_type":"SQ_LINK_TYPE_1",
	"link_value":"",
	"dataCallback":function(data){
		var imgFile;
		for (assetID in data){
			imgFile = assetID;
		}
		js_api.acquireLock({
		   "asset_id":imgFile,
		   "dependants_only":0,
		   "force_acquire":1,
		   "dataCallback":function(data){
		   		js_api.updateFileAssetContent({
				   	"asset_id":imgFile,
			   		"content":imgData, //some base64-encoded data
				   	"dataCallback":createComplete
				});
		   	}
		});
	}
});

It all works nicely, but I can't figure out how to get a trigger to run when my image has been created. I've set up a trigger to run on asset created and simple edit accessed which creates an image variety. The varieties create properly when I create images in the admin interface, but not when they're created via the JS API on the frontend.

 

Is there a different trigger action that I can use to fire when I'm finished with the updateFileAssetContent event?


(Anthony Ponomarenko) #2

Hi Phil,

 

Take a look at this example on the Manuals site. https://matrix-manuals.squiz.net/triggers/examples/configuring-a-trigger-to-create-an-image-variety#edit-plus 

 

Hopefully you can use that to get it going.

 

Cheers

Anthony


(Bart Banda) #3

Hey Phil, the asset created trigger event wont create ur varieties as it will try and run on your first JS API call when there is no image data. You need to make it run on the second call, which is the updateFileAssetContent. You would need to use something like asset accessed and check for a POST variable that matches that JS API function call and create your image varieties off that. 

 

Hope that makes sense. 


(Phil Mcphee) #4

Thanks guys. That does make sense Bart, but I'm not having any joy. I'm thinking maybe I'm not checking the POST variable in the right way. Here's my trigger - any ideas?

 

Event: Asset accessed

Conditions:

  • Tree location: [the website asset]
  • Asset is of type: Image
  • Keyword value: keyword %globals_post_type% is an exact match with updateFileAssetContent (This is based on type: "updateFileAssetContent" appearing in my header parameters for the request in my browser inspector)

Actions: Create image variety (with ignore permissions turned on)


(Bart Banda) #5

Hmm, i don't think that will work as the asset accessed will fire on the JS API asset and not on the actual image asset. 

So you need to have a way of exposing the asset ID from the post request, which you should be able to as it should be in the post data. 

 

Then use the Fire on other Assets feature of the trigger action to make the action fire on the image asset instead of the JS API asset.

 

You probably have to change the tree location and asset if of type condition to as they will be evaluated against the asset being accessed, which in this case is the JS API asset.