Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,18 @@ sort="desc"

## Tag Reference

> **Note:** `collection` and `event` are mutually exclusive. Pass `collection` to
> list occurrences from a whole collection, or `event` to get occurrences for a
> single entry — not both. Using them together throws an exception. (This does not
> apply to `events:download_link`, which accepts both.)

### events:between

Returns events within a date range.

**Parameters:**
- `collection` (optional, defaults to 'events')
- `event` (optional) Pass `id` of the event you want occurrences for
- `collection` (optional, defaults to 'events'; cannot be combined with `event`)
- `event` (optional) Pass `id` of the event you want occurrences for; cannot be combined with `collection`
- `from` (optional, defaults to now)
- `to` (required)

Expand All @@ -221,8 +226,8 @@ Additional flags:
Returns events within a future time window.

**Parameters:**
- `collection` (optional, defaults to 'events')
- `event` (optional) Pass `id` of the event you want occurrences for
- `collection` (optional, defaults to 'events'; cannot be combined with `event`)
- `event` (optional) Pass `id` of the event you want occurrences for; cannot be combined with `collection`

Example:

Expand All @@ -235,8 +240,8 @@ next="90 days"
Returns events occurring today.

**Parameters:**
- `collection` (optional, defaults to 'events')
- `event` (optional) Pass `id` of the event you want occurrences for
- `collection` (optional, defaults to 'events'; cannot be combined with `event`)
- `event` (optional) Pass `id` of the event you want occurrences for; cannot be combined with `collection`
- `ignore_past` (optional, defaults to 'false')

### events:upcoming
Expand All @@ -245,8 +250,8 @@ Returns the next set of event occurrences.

**Parameters:**
- `limit` (required)
- `collection` (optional, defaults to 'events')
- `event` (optional) Pass `id` of the event you want occurrences for
- `collection` (optional, defaults to 'events'; cannot be combined with `event`)
- `event` (optional) Pass `id` of the event you want occurrences for; cannot be combined with `collection`
- `collapse_multi_days` (optional)
- `offset` (optional)

Expand Down
7 changes: 6 additions & 1 deletion src/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TransformStudios\Events;

use Statamic\Facades\Entry;
use Statamic\Support\Arr;
use Statamic\Tags\Collection\Entries as BaseEntries;

class Entries extends BaseEntries
Expand All @@ -16,7 +17,11 @@ class Entries extends BaseEntries
protected function query()
{
$query = Entry::query()
->whereIn('collection', $this->collections->map->handle()->all());
->when(
$this->params->get('event'),
fn ($query, $id) => $query->whereIn('id', Arr::wrap($id)),
fn ($query) => $query->whereIn('collection', $this->collections->map->handle()->all())
);

$this->querySite($query);
$this->queryPublished($query);
Expand Down
5 changes: 5 additions & 0 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static function fromEntry(string $id): self

public function __construct(Parameters $params)
{
throw_if(
$params->has('collection') && $params->has('event'),
new Exception('The [collection] and [event] parameters cannot be used together.')
);

$this
->params($params) // gotta be first cuz some of the later one push to it
->collection($params->get('collection', 'events'))
Expand Down
23 changes: 23 additions & 0 deletions tests/Tags/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
test('can generate between occurrences of a specific event', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Entry::make()
->collection('events')
->slug('single-event')
->id('single-event')
->data([
'title' => 'Single Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'categories' => ['one'],
])->save();

$this->tag
->setContext([])
->setParameters([
Expand All @@ -43,6 +55,17 @@
expect($occurrences)->toHaveCount(4);
});

test('throws when both collection and event params are used', function () {
$this->tag
->setContext([])
->setParameters([
'collection' => 'events',
'event' => 'recurring-event',
]);

$this->tag->between();
})->throws(\Exception::class, 'The [collection] and [event] parameters cannot be used together.');

test('can generate between occurrences', function () {
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Expand Down
Loading