4

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.

8

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 %}
  • Thanks for the feedback but this is not what I'm after, please see my update above. – Matt V Apr 14 '15 at 18:10
  • See if that works better (my brain hurts). – Douglas McDonald Apr 15 '15 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" %} – medoingthings Apr 27 '16 at 10:07
  • 1
    @medoingthings: Shortened and fixed ;) Glad it helped. – Douglas McDonald Apr 29 '16 at 3:29
  • 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. – JonMack May 16 '17 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
  • Thanks Matt V. Somehow I missed your comment way back when. All fixed. – Douglas McDonald Apr 29 '16 at 3:30

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.