Looking to loop through and filter based on possible values in a text entry field...I have a text field called storeBanner that has 11 possible values (but could have more). Just need a way to go through every entry and create an array with all possible values.

  • Can you provide more information? What have you tried so far?
    – Lindsey D
    Jun 30 '15 at 16:57
  • Haven't tried anything yet. Not sure how best to go about that
    – brunam
    Jun 30 '15 at 16:59

I'm not sure about an array of the possible values. But you can get the actual values. You can do this with twig's merge filter. First get all the entries:

{% set allEntries = craft.entries.section('whatever') %}

Then loop over the entries and add them to an array:

{% set allValues = [] %}

{% for oneEntry in allEntries %}
  {% if oneEntry.storeBanner not in allValues %}
    {% set allValues = allValues | merge([oneEntry.storeBanner]) %}
  {% endif %}
{% endfor %}

Now allValues will be an array of the values

Things to notice:

  • You need to set allValues outside of the for-loop. Otherwise it will only exist in the scope of the for loop, and not afterwards - ouside it.
  • You need to pass an array as the parameter of merge. In this case an array with one value: [oneEntry.storeBanner]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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