Contact form and file upload - display images in data.{html|text}.twig

Hi,

On a site I’m developping, I exepect a to allow user to submit form in which they upload images files. It works fine but for the result message or the email sent to user, I would like to display url to images and not the array.

As it’s done by default, it shows me:

Nom: Form Sample
E-mail: me@me.com
Message: Sample Test
Prix: 450€
Photos: {"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/sYijyJu2BdF LKr5.jpg":{"name":"sYijyJu2BdFLKr5.jpg","type":"image\/jpeg","size":919863,"path":"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/sYijyJu2BdFLKr5.jpg"},"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/IQ0lvYXVC9hPLSi.jpg":{"name":"IQ0lvYXVC9hPLSi.jpg","type":"image\/jpeg","size":900292,"path":"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/IQ0lvYXVC9hPLSi.jpg"},"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/4LHR0Wg6AjedZJm.jpg":{"name":"4LHR0Wg6AjedZJm.jpg","type":"image\/jpeg","size":947550,"path":"user\/pages\/09.petites-annonces\/soumettre-une-annonce\/4LHR0Wg6AjedZJm.jpg"}}

For the photos section, I would li ke to have only:

Photos :
* http://www.site.url/<path_to_the_image_1>
* http://www.site.url/<path_to_the_image_2>
* http://www.site.url/<path_to_the_image_3>

So I started to play with forms/data.html.twig to have something like:

{% for index, field in form.fields %}
    {% set input = attribute(field, "input@") %}
    {% if input is null or input == true %}
        {% block field %}
            <div>
                {% block field_label %}
                    <strong>{{ field.label }}</strong>:
                {% endblock %}

                {% block field_value %}
                {% if field.type == "file" and field.name == "photos" %}
                	{% for item in photos %}
                	{{ string(item.path )|nl2br }}
                	{% endfor %}
                {% else %}
                    {{ string(form.value(field.name ?&quest; index))|nl2br }}
                {% endif %}
                {% endblock %}
            </div>
        {% endblock %}
    {% endif %}
{% endfor %}

But I can’t manage to get the path value of the array ; what did I miss ?

Thanks,
Nicolas

Ok, need to lunch as answer is obvious :slight_smile:

                {% block field_value %}
                {% if field.type == "file" %}
                	<ul>
                	{% for item in form.value(field.name) %}
                		<li><a href="/{{ string(item.path) }}">{{ item.name }}</a></li>
                	{% endfor %}
                	</ul>
                {% else %}
                    {{ string(form.value(field.name ?&quest; index))|nl2br }}
                {% endif %}
                {% endblock %}
1 Like

The default data.html.twig is a generic dump of the values saved. You already found out how to do it :slight_smile: