<p>Something like this should get you going in the right direction:</p>

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

<p>See Brandon's answer here for more information and documentation:
<a href="https://craftcms.stackexchange.com/questions/895/is-it-possible-to-use-the-elementtype-modal-ui-for-a-custom-field-type-plugin">Is it possible to use the ElementType Modal UI for a custom Field Type Plugin?</a></p>

CC BY-SA 3.0
2014-08-19T15:58:07.667Z
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
CC BY-SA 3.0
2014-08-20T02:59:59.467Z
This worked out great. Thanks for the response.
Justin Kimbrell
CC BY-SA 3.0
2014-08-20T03:01:15.120Z
Glad it worked out! I look forward to checking out the plugin when it's ready :-)
Peter Tell
CC BY-SA 3.0
2014-08-20T03:12:09.040Z
I just released the plugin today and just updated it with a couple features (using the code in this post). Check it out. https://github.com/objectivehtml/Google-Maps-for-Craft
Justin Kimbrell
CC BY-SA 3.0
2014-08-20T13:17:22.633Z
Wow, killer plugin Justin! Congrats on the release, it looks fantastic!
Peter Tell
CC BY-SA 3.0
2014-08-19T15:47:45.363Z
2014-08-19T15:47:45.363Z
2017-04-13T12:51:00.253Z
Peter Tell
<p>I build a plugin called <a href="https://craftpl.us/plugins/lemmings" rel="nofollow">Lemmings</a> that does this as one of it's built in functions.</p>

<p>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.</p>

<pre><code>{{ 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,
}) }}
</code></pre>

<p>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.</p>

<pre><code>    $variables['t_elementType'] = craft()-&gt;elements-&gt;getElementType(ElementType::Asset);
</code></pre>

CC BY-SA 3.0
2014-08-19T04:37:07.303Z
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
CC BY-SA 3.0
2014-08-19T14:46:31.663Z
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
CC BY-SA 3.0
2014-08-19T15:05:50.893Z
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
CC BY-SA 3.0
2014-08-19T03:23:14.690Z
2014-08-19T03:31:58.083Z
2014-08-19T03:31:58.083Z
Adam McCombs
<p>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.</p>

<plugin-development><custom-fieldtype>
How does a plugin make use of Assets?
# How does a plugin make use of Assets?

(Asked by: username_0 on 2014-08-19)

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.

**Tags:** plugin-development, custom-fieldtype

---

## 2 answers

### Answer by username_1 on 2014-08-19. Score: 2 | Accepted answer

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?

> **Comments:**
> **username_0**: 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.  
> **username_0**: This worked out great. Thanks for the response.  
> **username_1**: Glad it worked out! I look forward to checking out the plugin when it's ready :-)  
> **username_0**: I just released the plugin today and just updated it with a couple features (using the code in this post). Check it out. https://github.com/objectivehtml/Google-Maps-for-Craft  
> **username_1**: Wow, killer plugin Justin! Congrats on the release, it looks fantastic!  

---

### Answer by username_2 on 2014-08-19. Score: 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);

> **Comments:**
> **username_0**: 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.  
> **username_2**: 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!  
> **username_0**: 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.  

---

