3

I'm building a module with a form that includes an element select field that I want to restrict to specific volume. So far this is what I have:

{{ forms.elementSelectField({
    label: "Asset"|t('app'),
    id: 'images', 
    name: 'images',
    elementType: 'craft\\elements\\Asset',
    selectionLabel: "Choose"|t('app'),
    criteria: {
        kind: ['images']
    },
}) }}

I can see there is a 'sources' attribute, but I don't know how to structure the value. I've tried:

sources:[1] and the handle sources:['images'] without luck.

3
+50

I am not expert in Craft CMS but this is how it works for me:

Depending on the element type, source is different.

Look at this link how Craft CMS implements this:

https://github.com/craftcms/cms/blob/60881865f1b680c5aa5d4ad8f56d9a1ce3e452b0/src/fields/BaseRelationField.php#L842

Now how assets implement inputsource function:

https://github.com/craftcms/cms/blob/f83815cf28e7ce91edd501fc55ffd10f915a808e/src/fields/Assets.php#L550

so the format of sources for asset is like:

['folder:uid in volumefolders table']

and you can easily limit user to select assets from volumes you want.

|improve this answer|||||
  • 1
    Works like a dream, thank you so much for this – I'm just pretending to be a developer so only just learning how to wade through the Craft code to figure things out. – user3788089 Oct 29 '19 at 10:12
  • @user3788089 you're welcome. Glad to hear that. – user677900 Oct 29 '19 at 11:04

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.