certainlyakey
1/30/2016 - 3:06 PM

Get all values of a field across a section in Craft CMS (Sublime Text snippet)

Get all values of a field across a section in Craft CMS (Sublime Text snippet)

<snippet>
	<content><![CDATA[
{% set ${1:fieldval_arr} = [] %}
{% for ${5:loop-item} in craft.entries.section('${2:section-name}').${3:field-name}${4:.order('${3:field-name} desc')}.limit(0) %}
	{% if ${5:loop-item}.${3:field-name} not in ${1:fieldval_arr} %}
		{% set ${1:fieldval_arr} = ${1:fieldval_arr}|merge([${5:loop-item}.${3:field-name}]) %}
	{% endif %}
{% endfor %}

{% if ${1:fieldval_arr} %}
	<${6:ul}>
	{% for ${3:field-name} in ${1:fieldval_arr} %}
		<${7:li}>{{ ${3:field-name} }}</${7:li}>
	{% endfor %}
	</${6:ul}>
{% endif %}
]]></content>
	<tabTrigger>twig_allfieldvalues</tabTrigger>
	<scope>text.html.twig</scope>
	<description>Gets all distinct values of a field across a section</scope>
</snippet>