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

Getting the path of a file submitted through a form

Started by Muut Archive 10 years ago · 3 replies · 409 views
10 years ago

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:

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

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

So I edited data.html.twig:

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:

TWIG
(File) <a href='http://site.org/{{ string(form.value(field.name).file|e) }}'>{{ string(form.value(field.name).name|e) }}</a>

Thank you :)

10 years ago

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) }}

10 years ago

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.

10 years ago

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.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1370 9 years ago
Archive · by Muut Archive, 9 years ago
2 943 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1127 9 years ago