Craft CMS Asked by Ian Ebden on September 4, 2021
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>
Thanks for the assist. Just updated my Craft and all sorted.
Correct answer by Ian Ebden on September 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP