4

Within my plugin, I have a need to open an Assets sheet and pick a file that has been uploaded and make use of it in my plugin. How is this possible? I am looking for something similar to the EE version of Assets that will allow me to programmatically use Assets with JS.

2

Something like this should get you going in the right direction:

Craft.createElementSelectorModal('Asset', {
    multiSelect: true,
    criteria: { locale: this.elementLocale, kind: 'image' }
});

See Brandon's answer here for more information and documentation: Is it possible to use the ElementType Modal UI for a custom Field Type Plugin?

5
  • Thanks for that answer. That looks like it's in the direction I want to go. I'll give that a shot and mark your answer correct if it works. – Justin Kimbrell Aug 19 '14 at 15:58
  • This worked out great. Thanks for the response. – Justin Kimbrell Aug 20 '14 at 2:59
  • Glad it worked out! I look forward to checking out the plugin when it's ready :-) – Peter Tell Aug 20 '14 at 3:01
  • I just released the plugin today and just updated it with a couple features (using the code in this post). Check it out. github.com/objectivehtml/Google-Maps-for-Craft – Justin Kimbrell Aug 20 '14 at 3:12
  • Wow, killer plugin Justin! Congrats on the release, it looks fantastic! – Peter Tell Aug 20 '14 at 13:17
1

I build a plugin called Lemmings that does this as one of it's built in functions.

Inside your plugin (depending on exactly what kind of plugin you're building) you'll need to include an element field aimed add accessing assets. Here's the one I used for Lemmings.

{{ forms.elementSelect({
     elements: t_element,
     id: 'thumbId',
     name: 'thumbId',
     elementType: t_elementType,
     criteria: {"kind":[],"localeEnabled":null,"locale":"en_us"},
     sourceElementId: thumbId,
     jsClass: 'Craft.AssetSelectInput',
     addButtonLabel: 'Select Thumbnail',
     limit: 1,
}) }}

Before you can build your html form/page. You'll need to register a control panel route (registerCpRoutes) and point over to an action. This action will rending your template and including the element type as defined below.

    $variables['t_elementType'] = craft()->elements->getElementType(ElementType::Asset);
3
  • This is useful information, but this doesn't help me. I specifically need to know how to access it programmatically with JS. With EE you can instantiate an Assets.Sheet object (or something like that) and it will have several methods to interact with Assets client side. – Justin Kimbrell Aug 19 '14 at 4:37
  • Sorry I guess I misunderstood your question. The above forms.elementSelect would allow you to utilize the built in asset selector/uploader just as if you were creating an entry. Anything outside of that and you'd have to create a custom modal/js/form etc. using some variant of craft()->elements->getElementType(ElementType::Asset) and output. Good luck! – Adam McCombs Aug 19 '14 at 14:46
  • I wrote all the JS in my plugin with Backbone.Marionette. It's very JS intensive and already have all tools in place to make this happen. I basically just need a button or anchor that drops down the assets sheet and allows users to select their file. Then once selected, the file url will be passed back to my plugin where that value is saved. I don't even need the upload feature, just the Assets file picker. – Justin Kimbrell Aug 19 '14 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.