Getting the path of a file submitted through a form


I have a form with a file field. The form values are then sent in a e-mail.

The file value is shown like this:

Your file: {"\/en\/path\/form\/file.pdf":{"name":"file.pdf","type":"application\/ pdf","size":63842,"file":"user\/pages\/path\/form\/file.pdf","route":"\/en\/path\/form\/file.pdf"}}

I would like it to appear like this instead (showing only the file sub-value):

Your file: (File) user/pages/path/form/file.pdf

So I edited data.html.twig:

{% for field in form.fields %}
	{% block field %}
		{% if!='g-recaptcha-response' %}
				<div>{% block field_label %}<strong>{{ field.label }}</strong>{% endblock %}: {% block field_value %}
			{% if field.type=='file' %}
				(File) {{ string(form.value(|e) }}
			{% else %}
				{{ string(form.value(|e) }}
			{% endif %}
				{% endblock %}</div>
		{% endif %} 
	{% endblock %}
{% endfor %}

{{ string(form.value(|e) }} turns out to be empty; what would be the proper syntax to get this value? Eventually, I would like to make a link:

(File) <a href='{{ string(form.value(|e) }}'>{{ string(form.value(|e) }}</a>

Thank you :slight_smile:

You’re almost there! But the file value contains an array (to suit multiple uploads) so you need

{{ string(form.value(|first.file) }}

Thanks a lot, it’s working! I think the default data.html.twig could be changed so that it lists all the files uploaded, with a link to each of them.

data.html.twig is a very general file, which you can customize based on your needs. You can create a PR on Github to propose an improvement for it, maybe it can be a seconday one we can include.