I have a huge set of entries which have a lot of fields. Say, the entry type is a 'book' and fields are 'keywords', 'year', 'authors', etc.
Now I'm building a filter form which would allow user to output entries by field values. On frontend side this form contains generally inputs with datalist tags which should contain all of a specific field's values. For example, the datalist for the 'Year' search input should contain all years ever set for all the books on the site.
How do I do this efficiently, how do we list all existing values for a field? This answer proposes to loop through each entry every time and collect the field values used in entries in an array - but when I tried to do it I hit the memory exhausted problem.
An example code of the approach which leads to memory being exhausted:
{% set years = [] %}
{% for book in craft.entries.section('books').year.order('year desc').limit(0) %}
{% if book.year not in years %}
{% set years = years|merge([book.year]) %}
{% endif %}
{% endfor %}
{% for year in years %}
<li>{{ year }}</li>
{% endfor %}