1

I've created an entry form with an assets field. All works great, uploading to portfolio/{currentUser.id} directories. On edit forms I only want to list assets uploaded by the currentUser so the .uploader(currentUser) param on the asset query is perfect. BUT... when user uploads assets their id is not being recorded. I can see it listed as NULL in the assets database table. Consequently, the newly uploaded asset won't appear in the existing assets list for the currentUser. Make sense?

So yeah, basically I want front end form asset uploads to record the uploader's id.

Any thoughts much appreciated.

Here's the field in my form...

<form method="post" accept-charset="UTF-8" enctype="multipart/form-data" autocomplete="on" class="block" id="account">
{{ actionInput('users/save-user') }}
{{ csrfInput() }}
{{ hiddenInput('userId', currentUser.id) }}

{%- set field = craft.app.fields.getFieldByHandle('userPortfolio') %}
{%- if field|length %}
    {# include a hidden input first so Craft knows to update the existing value, if no checkboxes are checked #}
    {{ hiddenInput('fields[userPortfolio]', '') }}
    
    {# get existing assets for this user #}
    {% set possibleAssets = craft.assets()
        .volume('cdn')
        .uploader(currentUser)
        .kind('image')
        .orderBy('filename ASC')
        .withTransforms([
            { width: 100, height: 100 }
        ])
    .all() %}

    {# get the currently related asset IDs #}
    {% set relatedAssetIds = currentUser
        ? currentUser.userPortfolio.ids()
        : [] %}

    <div class="field mb-6">
        <label for="{{ field.id }}" class="label">{{ field.name }}{%- if field.instructions %} <span class="label-hint">{{ field.instructions }}</span>{%- endif %}</label>
        {%- if possibleAssets|length %}
            <p class="control">
                <table class="table is-fullwidth">
                    <thead>
                        <th class="is-narrow"><span class="is-hidden">Checkboxes</span></th>
                        <th class="is-narrow">Photo</th>
                        <th>Filename</th>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="3" class="has-text-grey-light">Uncheck an image and save the form to hide it on your public portfolio</td>
                        </tr>
                    </tfoot>
                    <tbody>
                        {%- for possibleAsset in possibleAssets %}
                            <tr>
                                <td class="is-narrow is-vcentered">
                                    {{ input('checkbox', 'fields[userPortfolio][]', possibleAsset.id, {
                                        checked: possibleAsset.id in relatedAssetIds
                                    }) }}
                                </td>
                                <td class="is-narrow is-vcentered">
                                    {{ possibleAsset.getImg({width: 48, height: 48}) }}
                                </td>
                                <td class="is-vcentered">
                                    {{ possibleAsset.filename }} {{ possibleAsset.folderId }}
                                </td>
                            </tr>
                        {%- endfor %}
                    </tbody>
                </table>
            </p>
        {%- endif %}

        {%- if currentUser.userPortfolio|length < 10 %}
            <p class="control">
                <div class="file is-fullwidth">
                    <label class="file-label">
                        <input type="file" name="fields[userPortfolio][]" class="file-input">
                        <span class="file-cta">
                            <span class="file-icon">
                                <i class="fas fa-upload"></i>
                            </span>
                            <span class="file-label">Upload a photo</span>
                        </span>
                    </label>
                </div>
            </p>

        {%- else %}
            <p>You have reached your maximum 10 allowed photos.</p>

        {%- endif %}
    </div>
{%- endif %}

<div class="field is-grouped">
    <p class="control">
        <button type="submit" name="submit" class="button is-link">Save Changes</button>
    </p>
    <p class="control">
        <a href="{{ url('account') }}" class="button is-light">Cancel</a>
    </p>
</div>
</form>
3
  • 1
    It would help to post your code? Jul 17 '20 at 15:59
  • Sure. I've added my code to the question.
    – Ian Ebden
    Jul 23 '20 at 14:19
  • I tried your code and can confirm what you're seeing. Just filed a bug report on your behalf. Jul 25 '20 at 19:13
2

Thanks for the assist. Just updated my Craft and all sorted.

1
  • Fixed in Craft CMS 3.4.30 - as of 7/27/20. Jul 28 '20 at 19:46

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.