Getting the path of a file submitted through a form

Hello,

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

{{ string(form.value(field.name).file|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='http://site.org/{{ string(form.value(field.name).file|e) }}'>{{ string(form.value(field.name).name|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(field.name)|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.