1

Newbie here.

First Craft 3 CMS build and fumbling with logic of Matrix blocks.

I've created a relatively simple matrix field, and I can output plain text and dropdown options, but not sure how to do a multiple choice field like checkboxes within a matrix.

Thanks

1

You'd use the checkbox fields much the same way they would be used if they were added directly to the entry and not in the matrix field. Assuming you've got a basic matrix field structure (using if/else or switch):

{% for block in entry.myMatrixField.all() %}

    {% switch block.type %}

        {% case "text" %}

            {{ block.textField | markdown }}

        {% case "image" %}

            {{ block.image[0].getImg() }}

        {% case "checkboxes" %}

            // HELP!!!

        {% default %}

            <p>A font walks into a bar.</p>
            <p>The bartender says, “Hey, we don’t serve your type in here!”</p>

    {% endswitch %}

{% endfor %}

You could use the same examples that are in the docs and just switch entry for the name of your block variable (block in our case):

Loop through the checked checkboxes:

{% for option in block.checkboxFieldHandle %}
    Label: {{ option.label }}
    Value: {{ option }} or {{ option.value }}
{% endfor %}

Loop through all of the available checkboxes:

{% for option in block.checkboxFieldHandle.options %}
    Label:   {{ option.label }}
    Value:   {{ option }} or {{ option.value }}
    Checked: {{ option.selected ? 'Yes' : 'No' }}
{% endfor %}

See if any checkboxes are checked:

{% if block.checkboxFieldHandle|length %}

See if a particular checkbox is checked:

{% if block.checkboxFieldHandle.contains('optionValue') %}
0

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.