Using ->groupBy() with a checkbox field

If I have a page with multiple values checked, it ends up in a group called “ValueOne, ValueTwo” when running through groupBy().

Would it be possible to instead make it appear in both “ValueOne” and “ValueTwo”'s group?

You could filter the page collection on whether it contains the “ValueOne” or “ValueTwo”.

$valueOnePages = $myCollection->filterBy('myfield', '*=', 'ValueOne');
$valueTwoPages = $myCollection->filterBy('myfield', '*=', 'ValueTwo');

With this solution it’s just checking whether the ValueOne or ValueTwo strings exist in the field’s raw value. Do mind that if your values are something like yes and yes-but-no, filtering on yes will give you false positives (matching both ValueOne and ValueTwo).

You can also filter using $myCollection->filter(function($page){ /* return true or false here*/ }; if you need to be more precise.

It won’t work with the groupBy() method. I haven’t tested the group($callback) method yet that will be available in 2.3. A workaround:

$values = $page->children()->pluck($field, ',', true);
foreach ($values as $value): ?>
<h2><?= $value ?></h2>
  <?php $filteredCollection = $page->children()->filter(function($child) use ($value) {
    return in_array($value, $child->$field()->split());
  }); ?>
  <?php foreach ($filteredCollection as $p): ?>
    // do stuff
  <?php endforeach ?>
<?php endforeach ?>

Thanks for the help, I will test it out.