Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

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

Started by Muut Archive 10 years ago · 2 replies · 875 views
10 years ago

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:

TXT
Nom: Form Sample
E-mail: [email protected]
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:

TXT
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:

TWIG
{% 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

10 years ago

Ok, need to lunch as answer is obvious :)

TWIG
                {% 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
10 years ago

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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1351 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4062 9 years ago
Archive · by Muut Archive, 9 years ago
1 2948 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago