3

I'm wondering if I might have a little too much inception madness in my thinking with this problem.

Enhancing a site we built for a conference last year. As there's an annual conference each year and some of the speakers are the same, we decided to reevaluate the approach.

What we have now is 2 channels, Speaker and Conference. Speaker contains all the speakers that could ever make an appearance at the conference and Conference contains the specific details about that conference.

Within conference, we have a Matrix field called Talks with a workshop, general and special event block.

I want to be able to group talk blocks by their block type, is that possible?

Going a step further, in the case of a workshop - that block has a workshopRound select field of 1, 2 or 3 - I'd love to be able to then group entries with those blocks by their round number.

Any thoughts on how this could be achieved?

Steven Grant
  • 1,855
  • 13
  • 27

1 Answers1

6

This is a great chance to use the group filter.

First, get all your matrix blocks as an array:

{% set talkBlocks = entry.talks.find() %}

Then group them by type:

{% set talksByCategories = talkBlocks|group('type') %}

At this point, you have an associative array where the keys are the block types, and the values are arrays of the blocks with that type. So talksByCategories.workshop or talksByCategories['workshop'] will be the workshops.

You can group the workshops by round:

{% set workshopsByRound = talksByCategories.workshop|group('workshopRound.value') %}

You should check that there were any workshops first (twig's defined test):

{% if talksByCategories['workshop'] is defined %} ...
Marion Newlevant
  • 12,047
  • 22
  • 55
  • thank you Marion, this looks a lot more succinct than the path I was initially looking at going down with http://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields mixed with http://craftcms.stackexchange.com/questions/9135/group-by-field-in-matrix-field

    The first part worked but the 2nd I was struggling with. I'll let you know how it goes. Thanks again.

    – Steven Grant Jun 15 '15 at 20:18