xpertbot / craft-wheelform

Craft CMS 4 Form with Database integration
MIT License
66 stars 32 forks source link

Get value in field #273

Closed alexanderloewe closed 2 years ago

alexanderloewe commented 2 years ago

Hello,

I'd like to create a list of all entries from all fields but only with the value of one field

  1. I think there is no variable for all entries, right? If not, do you plan to add this?

  2. how can i get the value of one specific field?

{% for entry in entries %} {{ entry.fields['entrypoint'].value }} {% endfor %}

(That dose not work)

xpertbot commented 2 years ago

Hello, Where are you trying to display the values?

alexanderloewe commented 2 years ago

In a login area for our team. So that they can work with the data.

It looks something like this:

<div class="inlinerow">     
    <table class="slimlist js-sort-table">
        <thead>
            <th>Entry</th>
            <th>Date</th>
        </thead>
        <tbody>
            {% for entry in form.entries %}
                <tr data-id="{{ entry.id }}">
                <td data-id="{{ current.name }}">{{ entry.fields['entrypoint'].value }}</td>
                    <td data-id="date">{{ entry.date | date("d.m.Y") }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
xpertbot commented 2 years ago

Is something like this what you're looking for? https://github.com/xpertbot/craft-wheelform#displaying-existing-form-submissions

You can also do specific entries using their ID in the URL and then collect it in the template and pass it to the Form Service.

alexanderloewe commented 2 years ago

Thanks for the link.

The code works, I had used this. I would now like to do 2 things which differ from the standard code.

I would like to display only the value of the entrypoint field in a table in a loop. The other values I do not need in the sense. Now the question, how can I get the value of a specific field within the entries?

What I have also tried is to display all entries of all forms in a single list. To display all entries in one list.

xpertbot commented 2 years ago

Hello @alexanderloewe, I think it's worth noting that Fields and Entries (Values) are independent from each other because a Form can be modified by the user and "new field" can be added which would not have any values saved, therefore making the field "useless" for older entries.

With that said, you can filter by field name and check to see if the field has any "entries" with that name. e.g.

{% set form = wheelform.form({ id: 1 }) %}

{# form.entries(start, limit) can be used for pagination purposes #}
{% set entries = form.entries %}

{# form.fields returns active form fields #}
{% set fields = form.fields %}

<table>
<thead>
    {% for field in fields %}
        {% if field.name == 'entrypoint' %}
            <th>{{ field.label }}</th>
        {% endif %}
    {% endfor %}
</thead>
<tbody>
    {% for entry in entries %}
        <tr data-id="{{ entry.id }}">
            {% for field in fields|filter((field) => field.name == 'entrypoint') -%}
                {% set current = entry.fields[field.name] %}
                <td data-id="{{ current.name }}">{{ current.value }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</tbody>
</table>

To display all the entries in all the forms you will have to get a bit more creative. Entries are attached to a Form every time so you can do a loop and see if the form exists. If the form id does not exists the "FormService" will throw an "Exception" Twig templates by default do not have try/catch handlers, your options are:

  1. you can use https://github.com/gglnx/twig-try-catch
  2. you can create your own module that handles the logic and displays them correctly.

I would personally go with the second option since it gives you more control over your data.

{% for form_id in range(1, 10) %}
    {% if wheelform.form({ id: form_id }) is defined %}
        {% set form = wheelform.form({ id: form_id }) %}
        {# display form entries here #}
    {% endif %}
{% endfor %}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.