1

In a front-end form where I am uploading a file, I need to specify a specific subfolder. I can't do it in the cp dynamic field for reasons so I was hoping to use a hidden field to set the folderId. The folderId exists and each entry has a different folder. I have this code that works but I can't figure out the code, for example something like: folderId: 44

<form method="post">
 {{ csrfInput() }}
 {{ actionInput('entries/save-entry') }}
 {{ hiddenInput('sectionId', '3') }}
 {{ hiddenInput('enabled', '1') }}
 {{ hiddenInput('entryId', entry.id) }}
 {{ hiddenInput('redirect', craft.app.request.absoluteUrl|hash) }}

    {% for relatedAssetId in entry.patientFiles.ids() %}
      {{ input(
          'hidden',
          'fields[patientFiles][]',
          relatedAssetId,
        ) }}
    {% endfor %}
          {{ input('file', 'fields[patientFiles][]', options={
             multiple: true,
          }) }}
    <button class="btn btn-primary" type="submit">Upload</button>

1 Answer 1

1

You can find a specific folder using the Assets service:

https://docs.craftcms.com/api/v3/craft-services-assets.html#public-methods

If you know all the necessary information when that template is parsed, the findFolder method might be useful but ensureFolderByFullPathAndVolume is probably more precise.

For example:

{{ hiddenInput( 'secret_folder_id', 
    craft.app.assets.ensureFolderByFullPathAndVolume($volumePath, $assetVolume) ) }}
2
  • Sorry, my request upon re-reading it might be vague. I have the folderId, the question is how tell the frontend form to make sure the asset gets uploaded specifically to that subfolder. I want something like {{ hiddenInput('folderId':44) }} but that does not work and the asset always just uploads to the parent volume, not the subfolder Apr 28, 2021 at 23:12
  • Ok, I think I know what you mean. You could try changing the properties of the asset field, checking "Restrict assets to a single folder" and specifying the path to the subfolder you'd like things to save to.
    – Wallace
    Apr 29, 2021 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.