5

We're looking to output a list of entries and group them by their check box values. Here's what we've got so far:

{% for checkbox, entries in craft.entries.section('my_entries').find() | group('checkbox') %}
   {{ checkbox }}
{% endfor %}

This currently returns "Object of class Craft\MultiOptionsFieldData could not be converted to string".

Any ideas? Is this even possible?

2 Answers 2

6

What Brad says, plus an example of grouping by selected checkbox:

{% set entries = craft.entries.section('my_entries') %}

{# get all checkbox options #}
{% set options = entries.first().checkbox.options %}

{% for option in options %}
    <h2>{{ option }}</h2>
    <ul>
    {% for entry in entries if entry.checkbox.contains(option) %}
       <li>{{ entry.title }}</li>
    {% endfor %}
    </ul>
{% endfor %}
1

In your example, checkbox is an instance of MultiOptionsFieldData, which behaves like an array of all of the possible checkbox options.

It doesn't have a __toString() method on it, which is why you're getting the error.

I assume what you want to be doing is grouping by a selected checkbox, no?

6
  • I expected to be able to do the a similar thing with categories, but think I'm running into the same thing: {% set entries = craft.entries.section('media').find() %} {% set entriesByCat = entries|group('mediaCategory') %} Can we just not use .group like this?
    – Tim Kelty
    Nov 21, 2014 at 15:33
  • I meant |group, as in the filter :)
    – Tim Kelty
    Nov 21, 2014 at 15:53
  • Added a related recipe: craftcookbook.net/recipes/330
    – Tim Kelty
    Nov 21, 2014 at 16:28
  • It would be nice if group was aware of Craft\ElementCriteriaModels, so you could do something like:{% set entriesByCat = entries|group('myCategory.slug') %}
    – Tim Kelty
    Nov 21, 2014 at 16:29
  • Though maybe that's a better case for a groupCriteria filter or something...ok I'll stop adding comments now.
    – Tim Kelty
    Nov 21, 2014 at 16:31

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.