<p>You can create a new field with these functions. Note: this is a very basic example, you would usually want to do this with a template but I don't have much time</p>

<pre><code>class FolderAssetField extends Field {
    // Static Methods
    // =========================================================================

    public $folderId;

    /**
     * Returns the display name of this class.
     *
     * @return string The display name of this class.
     */
    public static function displayName(): string{
        return 'Folder';
    }

    /**
     * @param $value
     * @param ElementInterface|null $element
     * @return string
     */
    public function getInputHtml($value, ElementInterface $element = null): string
    {
        $allFileModels = \craft\records\VolumeFolder::find()-&gt;all();
        $string = 'select a folder&lt;br&gt;&lt;select name="' . $this-&gt;handle .  '"&gt;';
        /** @var VolumeFolder $folder */
        foreach ($allFileModels as $folder){
            $selected = ((int)$value === $folder-&gt;id)? 'selected="selected' : '';
            $string .= '&lt;option ' . $selected .  ' value="' . $folder-&gt;id . '"&gt;' . $folder-&gt;name .  '&lt;/option&gt;';
        }
        $string .= '&lt;/select&gt;';

        return $string;
    }

}
</code></pre>

<p>Your field will then contain the <code>folderId</code> of the selected folder. You can then  get all assets for this folder with</p>

<pre><code>/** @var AssetQuery $assetQuery */
$assetQuery = \craft\elements\Asset::find();
$assets = $assetQuery-&gt;folderId($value)-&gt;all();
</code></pre>

<p>or in Twig</p>

<pre><code>{% set assets = craft.assets.folderId(entry.fieldHanlde).all() %}
{% for file in assets %}
    {{ file.title }}&lt;br&gt;
{% endfor %}
</code></pre>

<p>You could also use the <code>normalizeValue</code> function to prepare this Query. The <a href="https://github.com/craftcms/redactor" rel="nofollow noreferrer">Redactor</a> plugin has some <a href="https://github.com/craftcms/redactor/blob/v1/src/Field.php#L220" rel="nofollow noreferrer">good examples here</a> </p>

CC BY-SA 3.0
2018-02-27T15:51:20.007Z
2018-02-27T17:00:10.880Z
2018-02-27T17:00:10.880Z
Robin Schambach
<p>Is there a way to pick a folder from an asset field? </p>

<p>The goal is to render a dynamic gallery from an asset folder (which is on S3) instead of selecting all images inside a folder by hand.</p>

<p>Use case:
In an asset field, I select folder 'XYZ' and render a gallery from all images which are inside. If a user uploads a new image to the folder 'XYZ', the gallery will be updated automatically.</p>

<p>Problem: 
Currently, I can only handpick the images from a folder. If someone uploads a new image, he/she also has to pick the new image in the asset field. So it is not dynamic.</p>

<p>Thanks in advance!</p>

CC BY-SA 3.0
2018-02-27T14:59:38.483Z
You can create a custom field for that purpose, would that be an option for you?
Robin Schambach
CC BY-SA 3.0
2018-02-27T15:18:36.513Z
@RobinSchambach I am not sure. What field type would you use for that?
D.Steinel
CC BY-SA 3.0
2018-02-27T15:21:31.227Z
A custom field https://github.com/craftcms/docs/blob/v3/en/field-types.md. You want to create a select for your folder models and return all files that are in the folder with the selected id. It's basically really easy to extend Craft fields for that purpose
Robin Schambach
CC BY-SA 3.0
2018-02-27T15:26:40.650Z
@RobinSchambach ok thanks. I will give it a try. I am very new to PHP so let's see how this goes.
D.Steinel
<assets><image><assets-field>
Asset Field: Select a folder instead of all images
# Asset Field: Select a folder instead of all images

(Asked by: username_0 on 2018-02-27)

Is there a way to pick a folder from an asset field?

The goal is to render a dynamic gallery from an asset folder (which is on S3) instead of selecting all images inside a folder by hand.

Use case:
In an asset field, I select folder 'XYZ' and render a gallery from all images which are inside. If a user uploads a new image to the folder 'XYZ', the gallery will be updated automatically.

Problem:
Currently, I can only handpick the images from a folder. If someone uploads a new image, he/she also has to pick the new image in the asset field. So it is not dynamic.

Thanks in advance!

**Tags:** assets, image, assets-field

> **Comments:**
> **username_1**: You can create a custom field for that purpose, would that be an option for you?  
> **username_0**: @RobinSchambach I am not sure. What field type would you use for that?  
> **username_1**: A custom field https://github.com/craftcms/docs/blob/v3/en/field-types.md. You want to create a select for your folder models and return all files that are in the folder with the selected id. It's basically really easy to extend Craft fields for that purpose  
> **username_0**: @RobinSchambach ok thanks. I will give it a try. I am very new to PHP so let's see how this goes.  

---

## 1 answers

### Answer by username_1 on 2018-02-27. Score: 1 | Accepted answer

You can create a new field with these functions. Note: this is a very basic example, you would usually want to do this with a template but I don't have much time
class FolderAssetField extends Field {
    // Static Methods
    // =========================================================================

    public $folderId;

    /**
     * Returns the display name of this class.
     *
     * @return string The display name of this class.
     */
    public static function displayName(): string{
        return 'Folder';
    }

    /**
     * @param $value
     * @param ElementInterface|null $element
     * @return string
     */
    public function getInputHtml($value, ElementInterface $element = null): string
    {
        $allFileModels = \craft\records\VolumeFolder::find()->all();
        $string = 'select a folder';
        /** @var VolumeFolder $folder */
        foreach ($allFileModels as $folder){
            $selected = ((int)$value === $folder->id)? 'selected="selected' : '';
            $string .= '' . $folder->name .  '';
        }
        $string .= '';

        return $string;
    }

}

Your field will then contain the folderId of the selected folder. You can then  get all assets for this folder with
/** @var AssetQuery $assetQuery */
$assetQuery = \craft\elements\Asset::find();
$assets = $assetQuery->folderId($value)->all();

or in Twig
{% set assets = craft.assets.folderId(entry.fieldHanlde).all() %}
{% for file in assets %}
    {{ file.title }}
{% endfor %}

You could also use the normalizeValue function to prepare this Query. The Redactor plugin has some good examples here


---

