1

Using Drupal 9 webform to upload a file and attach to Civi contact. (This has worked well for D7 and Civi.)

D9 will not render the file upload element unless there is a private folder in Drupal. But then the uploaded file is not attached to Civi contact (using custom file field in Civi) but rather stored in Drupal private folder.

Is there a way to do this with D9 as it has worked for D7?

2
  • 1
    Do you need the file to be in a contact custom field or would it be enough for someone just to have a link to it? If the latter, then one option is have the webform create an activity on the contact, and choose one of the options like "Include link to view webform submission in activity details".
    – Demerit
    Commented Sep 26, 2023 at 23:55
  • Thanks, was able to get it to work as in D7 by removing a document element that was used in the webform before the File link elements. II can only guess as to why that made a difference but it did.
    – Dave T
    Commented Sep 27, 2023 at 16:58

2 Answers 2

2

As mentioned in comment, I was able to get it to work as expected/desired by creating a webform that started with only a Name and the elements to link file to the Civi contacts. Then I recreated the rest of the webform elements and dropped a document element. Again, not sure if the document element was the problem or some other change with the new webform creation.

1
  • We have several sites with D9 webforms including files - i don't recall hitting this issue but will ask around the Fuzion team - glad you found a workaround
    – petednz - fuzion
    Commented Sep 27, 2023 at 19:15
1

There is a way, at least if you are using the webform integration based on form processors and CMRF (and not the "built in CiviCRM <-> Webform" integration - not sure if it works there).

  • Create a form processor input oft the type "file"
  • Create a form processor action "Upload file to a custom field"

This allows the form processor to "fetch" any file if provided with an url and store it in CiviCRM. The problem is that drupal stores files in the private file system so the form procressor is not allowed to fetch the file by default.

You can work around that problem by submitting an url that contains an authorization token from Drupal to the form processor:

  • Create a corresponding file field in Drupal Webforms
  • In Drupal Webform configuration, navigate to the file field's settings for the submission display
  • in the dropdown, choose "custom"
  • in "Item format custom HTML" enter:

<a href="{{ item.url }}?token={{ token }}" target="_blank">{{ item.name }}</a>

This should pass an url to the form processor which allows it to "fetch" the file. I am not 100% sure if some global webform-settings or Drupal permissions need to be adapted as well but I think not.

All of this can be a bit hard to set up and debug but it should work. Make sure you have the latest releases of all components (Formprocessor, CMRF, Webform...).

Your Answer

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

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