Search code examples
subsetapostrophe-cms

apostrophe CMS dealing with subsets of pieces and relationships


Here is my event definition

module.exports = {
  extend: 'apostrophe-pieces',
  name: 'event',
  label: 'Event',
  pluralLabel: 'Events',
  addFields: [
    {
      name: 'eventName',
      label: 'Event Name',
      type: 'string',
      required: true
    },
    {
      name: 'location',
      label: 'Location',
      type: 'string',
      required: true
    },
    {
      name: 'date',
      label: 'Date',
      type: 'date',
      required: true
    },
    {
      name: 'thumbnail',
      label: 'Thumbnail',
      type: 'singleton',
      widgetType: 'apostrophe-images',
      options: {
        limit: 1,
        minSize: [ 200, 200 ],
        aspectRatio: [ 1, 1 ]
      }
    }
  ]
};

How can I use a subset of only the 5 nearest future events on a page?

Additionally if I was going to do something similar with customers could I use different subsets of them on different pages based on an string field that identified their industry?


Solution

  • If you use the apostrophe-events npm module, rather than rolling this yourself, what you are asking for will be the default behavior of the apostrophe-events-widgets module (showing the next 5 events that haven't ended).

    If you wanted to do this yourself, you could do so by setting the sort option of your events module to { date: 1 }, and adding a custom cursor filter to ignore events whose date is in the past when invoked. Then you would invoke that filter method from your events-widgets module.

    But, since apostrophe-events does all this for you with added provisions for correctly handling start and end dates if you need them, I would not recommend "winging it."

    Just to be complete though, you can see an implementation of the "upcoming" cursor filter for apostrophe-events here:

    https://github.com/apostrophecms/apostrophe-events/blob/master/lib/cursor.js

    And, here's the logic in apostrophe-events-widgets to use that filter to show only events that are upcoming in that widget, which you can add to your home page to get the effect you want:

    https://github.com/apostrophecms/apostrophe-events/blob/master/lib/modules/apostrophe-events-widgets/index.js

    The apostrophe-events-pages module (part of the apostrophe-events bundle) also uses this filter by default but supports browsing by date as well, which overrides it to give access to past events.