filterBy Structured Field

Hey all,

I apologize if this question has been asked before. I dug around and didn’t find a topic that seemed to answer my particular question.

Background: I am build a site for a law firm. They want to showcase their cases on their site, and display their wins and losses on a page within two different content wells.

I’ve approach this by making a custom field in the cms, which has radio options for ‘won’ and ‘lost’.

My question is: can I use filterBy to gather all of the wins in one area of the page, and losses in another? Right now my code looks like this, but of course it doesn’t work.

        <?php $caseList = yaml($page->caseList()) ?>
        <?php foreach($caseList->filterBy('caseresult','won') as $case): ?>  
          <h1><?php echo $case['casetitle'] ?></h1>
        <?php endforeach ?>

Additionally, I’m wondering if Kirby has a method of counting a total number based on the chosen option, so I can dynamically display their % of wins. eg: 70% won

I dont think, that the solution you want is possible with the filterBy method, because this method only works on a collection of files or pages. (correct me if I’m wrong).
But by having some yaml encoded text with all cases and parsing them with the yaml method, you will get an (multi-dimensional) array with your cases. This is not the same as the collections of page or file objects Kirby uses.
If you have a collection of pages, you could folder them by some field with filterBy and count them with count.

In your case, you should search for filtering arrays by some key with php itself.

I think it makes more sense anyway to split them into two fields in the first place. So you would have one structure field for wins and one for losses.

Ha, you’re totally right. I’m actually already doing that in another area of the site, silly I didn’t think of this.