1

I need to be able to get a count (in the frontend) of the number of submissions that have a field with a specific value. I don't need the submitted data of the submissions, just the count of submissions whose data matches a specified value.

This code returns the total number of submissions:

{% set submissionCount = craft.freeform.submissions({
    form: 'myAmazingForm'
}).count %}

I tried using the native Craft search parameter, but it returns zero:

{% set submissionCount = craft.freeform.submissions({
    form: 'myAmazingForm'
}).search('fieldWithValue:123').count %}

And yes, there are submissions that would match that.

How can I do this?

3 Answers 3

1

This might not work, but you can try it like so:

{% set submissionCount = craft.freeform.submissions({
    form: 'myAmazingForm',
    fieldWithValue: '123',
}).count() %}
1

Unfortunately this currently isn't possible. However, we've added it to the feature requests list and will revisit adding this in the not-too-distant future. :)

5
  • 1
    Drat! I'm converting an EE site that uses that Freeform feature and had expected to be able to do the same things. :-/
    – Sandwich
    Commented Dec 21, 2017 at 19:33
  • @solspace I'm just curious... It took me only a few minutes to create that feature with a custom function and a search via Yii Records, why don't you present a workaround even just for this special use case - instead of saying not possible currently? Commented Dec 21, 2017 at 21:28
  • I'd love to see a temporary workaround for this...?
    – Sandwich
    Commented Dec 24, 2017 at 11:53
  • Is this possible right now? @Solspace Commented May 10, 2019 at 12:21
  • An upcoming version will include this (late May 2019)
    – Solspace
    Commented May 13, 2019 at 3:36
1

Here is the workaround I've resorted to using in the meantime.

{# Initiate the hash in which we'll store matching submissions. #}
{% set matchingSubmissions = [] %}

{#
    Loop through all submissions, checking if the specified field
    matches the desired value.
#}
{% for submission in submissions if submission.fieldWithValue == '123' %}

    {# If the field matches, add it to the hash #}
    {% set matchingSubmissions = matchingSubmissions|merge([submission.fieldWithValue]) %}

{% endfor %}

{# Count the size of the resulting hash and save it. #}
{% set submissionCount = matchingSubmissions|length %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.