<p>To have asset selection and upload in your twig, you can use <a href="https://github.com/craftcms/cms/blob/develop/src/templates/_includes/forms/elementSelect.html" rel="nofollow noreferrer">elementSelectField</a></p>
<p>the code i wrote here is just sample and definitely you need to review, consider security and validation</p>
<p>firstly you should have an extra public attribute to keep selected asset on settings model:</p>
<pre><code>class Settings extends Model
{
    .....
    public $imageId = [];
    .....
</code></pre>
<p>in your settings.twig template you use elementSelectField like:</p>
<pre><code>{{ forms.elementSelectField({
    label: &quot;image&quot;,
    id: &quot;imageId&quot;,
    name: &quot;imageId&quot;,
    viewMode: &quot;list&quot;,
    elementType: 'craft\\elements\\Asset', //limit to to asset element type
    selectionLabel: &quot;Add Images&quot;,
    sources: ['folder:'~folderUid], //if you want to limit it to one volume folder not all volumes
    required: false,
    errors: settings.getErrors('imageId'),
    limit: 2, // if you want maximum two images 
    elements: images,  // at first it is null, then we pass selected assets as images 
    criteria: { kind: 'image'} // because you want only images
}) }}
</code></pre>
<p>in settingsHtml() method:</p>
<pre><code>use craft\records\VolumeFolder;
protected function settingsHtml()
{
    $settings = $this-&gt;getSettings();
    // to load currently selected assets 
    $images = [];
    if ($settings-&gt;imageId) {
        foreach ($settings-&gt;imageId as $imgId) {
            $images[] = Craft::$app-&gt;elements-&gt;getElementById($imgId);
        }
    }
    //to limit asset selection to one folder
    $volume = Craft::$app-&gt;volumes-&gt;getVolumeByHandle('volumeHandle');//it could be the volume handle you created before
    $volFolderRecord = VolumeFolder::findOne([
        'volumeId' =&gt; $volume-&gt;id
    ]);
    $folderUid = $volFolderRecord-&gt;uid;
    return \Craft::$app-&gt;getView()-&gt;renderTemplate(
        'plugin-handle/settings',[ 
            'settings' =&gt; $settings,
            'images' =&gt; $images
            'folderUid' =&gt; $folderUid
     ]);
</code></pre>

CC BY-SA 4.0
2022-02-11T13:22:17.430Z
@DTS as i said in my answer this is only a sample. but have you copied exact code without any change? and which line of code has error?
user9975
CC BY-SA 4.0
2022-02-12T17:41:36.107Z
@dts something is wrong with $setting. maybe you overwritted it. try to do craft::dd($settings) after $this->getSettings(); and also before $settings->imageId and see what it returns.
user9975
CC BY-SA 4.0
2022-02-14T07:07:01.743Z
Thank you for the response. Happy to hear from you. I will definitely Look into it.
DTS
CC BY-SA 4.0
2022-03-01T11:45:16.803Z
How to save the selected assets values in this??
DTS
CC BY-SA 4.0
2022-03-01T12:27:47.907Z
@DTS in this case, no need to do anything. Craft does it. you can get image ids in your controller from plugin setting with $imageids = \yournamespace\Plugin::getInstance()->getSettings()->imageId;
user9975
CC BY-SA 4.0
2021-12-24T12:26:23.217Z
2021-12-27T15:00:35.773Z
2021-12-27T15:00:35.773Z
user9975
<p>I am creating a plugin that is based on images the admin will upload some sort of images in plugin settings not sure how to creat image field setting in plugin</p>

<craft3><plugin-development><templating><assets>
Want to use assets field my template settings in custom plugin craftcms
# Want to use assets field my template settings in custom plugin craftcms

(Asked by: username_0 on 2021-12-21)

I am creating a plugin that is based on images the admin will upload some sort of images in plugin settings not sure how to creat image field setting in plugin

**Tags:** craft3, plugin-development, templating, assets


---

## 1 answers

### Answer by username_1 on 2021-12-24. Score: 2

To have asset selection and upload in your twig, you can use elementSelectField
the code i wrote here is just sample and definitely you need to review, consider security and validation
firstly you should have an extra public attribute to keep selected asset on settings model:
class Settings extends Model
{
    .....
    public $imageId = [];
    .....

in your settings.twig template you use elementSelectField like:
{{ forms.elementSelectField({
    label: "image",
    id: "imageId",
    name: "imageId",
    viewMode: "list",
    elementType: 'craft\\elements\\Asset', //limit to to asset element type
    selectionLabel: "Add Images",
    sources: ['folder:'~folderUid], //if you want to limit it to one volume folder not all volumes
    required: false,
    errors: settings.getErrors('imageId'),
    limit: 2, // if you want maximum two images 
    elements: images,  // at first it is null, then we pass selected assets as images 
    criteria: { kind: 'image'} // because you want only images
}) }}

in settingsHtml() method:
use craft\records\VolumeFolder;
protected function settingsHtml()
{
    $settings = $this->getSettings();
    // to load currently selected assets 
    $images = [];
    if ($settings->imageId) {
        foreach ($settings->imageId as $imgId) {
            $images[] = Craft::$app->elements->getElementById($imgId);
        }
    }
    //to limit asset selection to one folder
    $volume = Craft::$app->volumes->getVolumeByHandle('volumeHandle');//it could be the volume handle you created before
    $volFolderRecord = VolumeFolder::findOne([
        'volumeId' => $volume->id
    ]);
    $folderUid = $volFolderRecord->uid;
    return \Craft::$app->getView()->renderTemplate(
        'plugin-handle/settings',[ 
            'settings' => $settings,
            'images' => $images
            'folderUid' => $folderUid
     ]);

> **Comments:**
> **username_1**: @username_0 as i said in my answer this is only a sample. but have you copied exact code without any change? and which line of code has error?  
> **username_1**: @dts something is wrong with $setting. maybe you overwritted it. try to do craft::dd($settings) after $this->getSettings(); and also before $settings->imageId and see what it returns.  
> **username_0**: Thank you for the response. Happy to hear from you. I will definitely Look into it.  
> **username_0**: How to save the selected assets values in this??  
> **username_1**: @username_0 in this case, no need to do anything. Craft does it. you can get image ids in your controller from plugin setting with $imageids = \yournamespace\Plugin::getInstance()->getSettings()->imageId;  

---

