I grab some values via an element query:

{% set events = craft.entries.section('events').status('live,pending').order('postDate asc') %}

But I need to put them into an array for use in a Craft-styled form field.

Twig documentation shows how to put text values in an array and merge them:

{% set items = items|merge({ 'peugeot': 'car', 'renault': 'car' }) %}

But how do I do this with the results of my element query?

1 Answer 1


Just put the field names in parentheses:

{% set options = [] %}
{% for event in events %}
  {% set options = options|merge({ (event.slug):(event.eventTitle) }) %}
{% endfor %}

And now I can output them in the format Craft wants for a form field:

{{ forms.selectField({
  label: "Choose event"|t,
  name: "eventSlug",
  options: options,
}) }}

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.