1

I have a section Events with a matrix field (let's call it dates). The matrix features a single block type with a single date field. One Event can have multiple dates. Think theatre plays with multiple show dates.

Now I'd like to create a list of all events, ordered by the date field. This means that the same entry could show multiple times in the list (once for each date in the matrix field). I want to query all future dates in the matrix, fetch related entries, limit the number of items displayed and support pagination.

Ideally there is something like craft.entries but for matrix fields.

Example

The following example shows three different Event entries, ordered by their dates. Notice how the same entry can appear multiple times.

  1. 14/12/01 @8pm - The Great Gatsby (ID #1)
  2. 14/12/01 @8pm - Moby Dick (ID #2)
  3. 14/12/02 @3pm - Snow White (ID #3)
  4. 14/12/02 @5pm - Snow White (ID #3)
  5. 15/12/05 @7pm - The Great Gatsby (ID #1)

Can anyone point me in the right direction? I'm not afraid to dive into plugin development (already created a simple "Address" field type with geocoding support) but is there an easier way? Thanks for your time!

4
  • Thanks @JoshAngell for the spelling corrections. English is not my first language as you might be able to tell. :)
    – michaelhue
    Nov 12, 2014 at 13:06
  • No it is very well written - in fact that was why I edited it :) Nov 12, 2014 at 13:08
  • @michaelhue To be fair, it's only "theatre" in the U.K. I would have left it as "theater". :)
    – Brad Bell
    Nov 12, 2014 at 15:56
  • @BradBell OK WE'RE WEIRD. Nov 15, 2014 at 12:43

3 Answers 3

1

You can do this using the Lowblocks plugin:

https://github.com/low/lowblocks

Im using it for exactly the same thing.

Brandon Kelly has said he's working on an enhancement in 2.3 to allow filtering Matrix fields by date.

In the meantime I used this temporary workaround...

{% set allDates = craft.lowblocks.blocks.fieldId(136).type('mbDate').order('mfDate').limit(null) %}

{% set upcomingIds = [] %}

{% for item in allDates if item.mfDate > now %}

    {% set upcomingIds = upcomingIds | merge([item.id]) %}

{% endfor %}

{% set upcomingDates = craft.lowblocks.blocks.id(upcomingIds).fieldId(136).type('mbDate').order('mfDate').limit(30) %}
4
  • The plugin seems to be exactly what I need! I have only one problem using it: I'd like to query only blocks with a future date value. I tried using craft.lowblocks.blocks.fieldId(12).date('>= NOW()') but I get a Craft\ElementCriteriaModel and its behaviors do not have a method or closure named "date" exception. Ordering block using the date field works however: craft.lowblocks.blocks.fieldId(12).order('date DESC'). Can you give me an example how you did it?
    – michaelhue
    Nov 14, 2014 at 11:08
  • Sorry I forgot about that bit. I've edited my answer to include a link to a comment by BK who's said he's working on this issue for 2.3, and also a temporary workaround I used. Hope it helps. Nov 15, 2014 at 12:30
  • Thanks for the workaround! Looking forward to the updated version of the Lowblocks plugin but for now, this is the best answer to my problem. Thank you Jamie!
    – michaelhue
    Nov 17, 2014 at 8:37
  • Just wanted to note that with version 2.3 of Craft this workaround is no longer necessary. > It is now possible to set custom field parameters on Matrix block queries.
    – michaelhue
    Dec 4, 2014 at 19:03
1

I think that in order to do this you are going to need to loop through all events; find eventTimes (within the blocks) that are after the selected date; merge those into an array; and then return the sorted array. However, by doing so you will loose the ability to easily use pagination.

Perhaps another solution would be to not use matrix blocks at all and instead create two channels: 1 for 'Shows' and 1 for 'Showtimes' (using your theatre analogy), where 'Showtimes' would be related to 'Shows' using an Entries field-type with limit set to 1. Then finding showtimes would be trivial, and would support pagination, etc. (You could even set the showtimes title to be auto-generated from 'related show title + datetime'.)

And if you wanted to display all the showtimes collected nicely on the Shows entry (for the sake of the client), you can use a nice plugin called 'introvert' to show reverse related entries.

1
  • 1
    Thanks for your suggestions! Looping through all events and replicating the pagination behavior is a last resort for me. Two channels would be nice from a data normalizing view but I'm afraid it will be too much of a hassle for the authors. Introvert looks useful, I will keep it in mind! Thanks again!
    – michaelhue
    Nov 14, 2014 at 11:02
1

If the native solutions seem a bit involved, or if you want some additional calendar processing power for your events (recurring events, calendar display, etc.), there's also Craft Calendars: http://michaelrog.com/craft/calendars

3
  • Thanks for the suggestion Michael! Your plugin seems very useful but I think it's a bit overkill for my purposes.
    – michaelhue
    Nov 23, 2014 at 17:16
  • Very interested to test your plugin Michael. Tell me if still possible to test please? Aug 16, 2015 at 0:31
  • Sure — [email protected] Aug 20, 2015 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.