6

I have a matrix field with a block called "employment", under this block are fields, one of those fields is a drop down.

I'd like to be able to output the drop down field as a select list so users can choose from it.

Note: that I'm NOT trying to output what a user has selected, I just want to output the options from which a user can select.

Thanks!

UPDATE

That's not really what I'm after. Let me be more clear.

The matrix field I'm speaking of is attached to the user model but I'm not trying to output the user's data saved for the matrix. What I'm trying to do is simply output a field of a given matrix.

I am able to load the matrix doing this:

{% set matrix = craft.fields.getFieldByHandle('employmentMatrix') %}

But I'm not sure how to access the matrix's blocks then fields. I tried this:

{% set matrix = craft.fields.getFieldByHandle('employmentMatrix') %}

{% set blocks = craft.matrix.getBlockTypesByFieldId(matrix.id) %}

But I get an error saying "matrix" is not a method.

3 Answers 3

9

Assuming you have an entry to work with, something like this should work:

{% for block in entry.myMatrixFieldHandle if block.type == "employment" %}
    <select>
        {% for option in block.myDropDownFieldHandle.options %}        
            <option value="{{ option.value }}">{{ option.label }}</option>
        {% endfor %}
    </select>
{% endfor %}

Otherwise you may need to get the field through the fieldLayout or by it's handle.

Update

craft.matrix is not exposed to twig like it is in php. To get the dropdown field options through the matrix field directly you'll have to go the long way around (matrix > blocktypes > blocktype > fields > field > options)

{% set matrixField = craft.fields.getFieldByHandle('employmentMatrix') %}
{% set blockTypes = matrixField.getFieldType.getSettings.getBlockTypes %}
{% for blockType in blockTypes if blockType.handle == "myBlockTypeHandle" %}
    {% for field in blockType.getFields() %}
        {% if field.handle == "myDropDownFieldHandle" %}
            <select>
                {% for option in field.settings.options %}
                    <option value="{{ option.value }}">{{ option.label }}</option>
                {% endfor %}
            </select>
        {% endif %}
    {% endfor %}
{% endfor %}

UPDATE: This is for craft 2. For craft 3 see Nathan S answer below.

6
  • Thanks for the feedback but this is not what I'm after, please see my update above.
    – Matt V
    Commented Apr 14, 2015 at 18:10
  • See if that works better (my brain hurts). Commented Apr 15, 2015 at 17:26
  • That saved me a real headache, thanks! Could be a little shortened by adding the condition directly to the for loop. E.g.: {% for blockType in blockTypes if blockType.handle == "myBlockTypeHandle" %} Commented Apr 27, 2016 at 10:07
  • 1
    @medoingthings: Shortened and fixed ;) Glad it helped. Commented Apr 29, 2016 at 3:29
  • 1
    This kind of stuff really needs to be in the Craft documentation. Thank you for your contribution, it just got me out of a real jam.
    – JxM
    Commented May 16, 2017 at 15:50
4

There's an error in Douglas McDonald's answer, here is the correction:

        {% set matrixField = craft.fields.getFieldByHandle('employmentMatrix') %}
        {% set blockTypes = matrixField.getFieldType.getSettings.getBlockTypes %}
        {% for blockType in blockTypes %}
            {% if blockType.handle == "myBlockTypeHandle" %}
                {% for field in blockType.getFields() %}
                    {% if field.handle == "myDropDownFieldHandle" %}
                        <select>
                            {% for option in field.settings.options %}
                                <option value="{{ option.value }}">{{ option.label }}</option>
                            {% endfor %}
                        </select>
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% endfor %}

The error was, in order to access a field's options you have to do this:

... field.settings.options NOT field.options
1
  • Thanks Matt V. Somehow I missed your comment way back when. All fixed. Commented Apr 29, 2016 at 3:30
4

Craft 3 updated the code so this is the new way to get a matrix field that worked for me. The only difference is matrixField.blockTypes vs Craft 2 matrixField.getFieldType.getSettings.getBlockTypes

 {% set matrixField = craft.fields.getFieldByHandle('employmentMatrix') %}
        {% set blockTypes = matrixField.blockTypes %}
        {% for blockType in blockTypes %}
            {% if blockType.handle == "myBlockTypeHandle" %}
                {% for field in blockType.getFields() %}
                    {% if field.handle == "myDropDownFieldHandle" %}
                        <select>
                            {% for option in field.settings.options %}
                                <option value="{{ option.value }}">{{ option.label }}</option>
                            {% endfor %}
                        </select>
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% endfor %}
1
  • Has anyone done similar yet for Craft 4?
    – mmc501
    Commented Aug 5, 2022 at 10:08

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.